所需的请求部分'config'不存在节点提取



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

curl -X POST    http://localhost:7200/test    -H 'Content-Type: multipart/form-data'    -F "config=@test.ttl"

到目前为止,我通过POST请求将文件作为formdata发送,就像curl请求一样。但是,我不知道如何在请求中包含config=@test.ttl。我试着把它添加到一个headers对象中,但我得到了无效字符的错误,所以我不确定把它放在哪里。当我按照下面的方式运行请求时。我得到了'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));

感谢

谢谢@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));

相关内容

最新更新