我正在尝试发送一个post请求使用服务将文档转换为pdf。该服务与邮递员一起工作得很好,y可以将文档发送到端点,它会返回给我pdf,很好。
但是我不能从我的nodejs服务器发出请求,我使用axios发出请求,它失败了,错误是:
{"time":"2019-09-24T14:39:46.89404124Z","id":"","remote_ip":"000.00.000.00","host":"pdf-doc-convert.example","method":"POST","uri":"/convert/office","user_agent":"axios/0.19.0","status":500,"error":"getting multipart form: no multipart boundary param in Content-Type","latency":221460,"latency_human":"221.46µs","bytes_in":0,"bytes_out":36}这是服务文档,是一个带有multipart/form-data请求的简单post (附带一个curl示例):
https://thecodingmachine.github.io/gotenberg/#office
这是我对axios的要求:
async function request() {
const endpoint =
"http://pdf-doc-convert.example/convert/office";
const data = new FormData();
data.append('files', fs.createReadStream("my/file/path/example.docx"));
const config = {
headers: {
"content-type": "multipart/form-data"
}
};
const pdf = await axios.post(endpoint, data, config);
}我怎样才能提出请求?
发布于 2019-09-25 00:38:18
也许最快的调试方法是使用Postman intercept截取您正在使用Axios请求进行的调用,并比较正在工作的和不工作的cURL请求信息。可能是头文件问题,也可能是文件编码问题。
我以前遇到过类似的情况,这可能与需要在Axios中进行额外配置的formData标头有关:https://github.com/axios/axios/issues/789#issuecomment-508114703
const data = new FormData();
data.append("firstFile", fs.createReadStream(firstFilePath), { knownLength: fs.statSync(firstFilePath).size });
const headers = {
...data.getHeaders(),
"Content-Length": data.getLengthSync()
};
const endpoint = "http://pdf-doc-convert.example/convert/office";
const pdf = await axios.post(endpoint, data, config);https://stackoverflow.com/questions/58083108
复制相似问题