所需的请求部分“配置”不存在节点获取

问题描述 投票:0回答:1

我正在尝试将此curl命令转换为节点获取请求。

curl -X POST    http://localhost:7200/test    -H 'Content-Type: multipart/form-data'    -F "[email protected]"

到目前为止,我通过 POST 请求将文件作为 formdata 发送,就像curl 请求一样。但是,我不确定如何在请求中包含

[email protected]
。我尝试将其添加到标头对象中,但收到无效字符错误,因此我不确定将其放在哪里。当我按如下方式运行请求时。我回来了
'Required request part 'config' is not present'
所以肯定需要把
config
放在某个地方。

const fetch = require("node-fetch");

const FormData = require('form-data');
const form = new FormData();  
form.append('test.ttl', 1);

fetch('http://localhost:7200/test', { 
        method: 'POST', 
        body: form 
    })
        .then(res => res.json())
        .then(json => console.log(json));

谢谢

node.js curl node-fetch
1个回答
0
投票

谢谢@bato3

为文件中的数据创建一个 readStream 是有效的。如果有人想在将来引用它,请在下面发布工作代码。

const fetch = require("node-fetch");
const fs = require('fs');
const FormData = require('form-data');

const form = new FormData();

form.append('config', fs.createReadStream('test.ttl'));

fetch('http://localhost:7200/test', { 
    method: 'POST', 
    body: form 
})
    .then(res => res.text())
    .then(body => console.log(body));
© www.soinside.com 2019 - 2024. All rights reserved.