我打算将下面的curl
API调用翻译为node.js
,但我在执行 POST时仍然会获得error
curl -X POST --user user1@customer1:secret http://demo.test.com/controller/actions/38 -F file=@ExportActions.json
下面是使用request-npm模块的初始代码,但API调用仍然失败。
var requestdata = fs.readFileSync('./ExportActions.json').toString();
var request = require('request');
request.post({
url: 'https://demo.test.com/controller/actions/38',
auth: {
'user': 'user1@customer1',
'pass': 'secret'
},
body: requestdata
}, function(error, response, body){
console.log(body);
});
每次运行脚本时,我都会收到以下错误:
Could not import Actions: org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
使用npm request
模块发布 JSON 文件时是否有变通办法?
谢谢!
答案 0 :(得分:0)
您正试图通过将其发布到正文来上传到文件。看起来服务器想要多部件上传。这可能效果更好
var req = require('request');
request.post({
url: 'https://demo.test.com/controller/actions/38',
auth: {
'user': 'user1@customer1',
'pass': 'secret'
},
}, function(error, response, body){
console.log(body);
});
var form = req.form()
form.append('file', fs.readFileSync('./ExportActions.json'));
答案 1 :(得分:-1)
您可以使用Postman客户端检查此api而不是curl。