首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >axios多部分请求失败

axios多部分请求失败
EN

Stack Overflow用户
提问于 2019-09-24 22:56:05
回答 1查看 360关注 0票数 0

我正在尝试发送一个post请求使用服务将文档转换为pdf。该服务与邮递员一起工作得很好,y可以将文档发送到端点,它会返回给我pdf,很好。

但是我不能从我的nodejs服务器发出请求,我使用axios发出请求,它失败了,错误是:

代码语言:javascript
复制
{"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的要求:

代码语言:javascript
复制
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);
}

我怎样才能提出请求?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-25 00:38:18

也许最快的调试方法是使用Postman intercept截取您正在使用Axios请求进行的调用,并比较正在工作的和不工作的cURL请求信息。可能是头文件问题,也可能是文件编码问题。

我以前遇到过类似的情况,这可能与需要在Axios中进行额外配置的formData标头有关:https://github.com/axios/axios/issues/789#issuecomment-508114703

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58083108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档