axios是否等同于http请求?

l7mqbcuq  于 2022-11-29  发布在  iOS
关注(0)|答案(1)|浏览(202)

我想发出以下http请求

POST /v1/images HTTP/1.1
Host: api.medium.com
Authorization: Bearer 181d415f34379af07b2c11d144dfbe35d
Content-Type: multipart/form-data; boundary=FormBoundaryXYZ
Accept: application/json
Accept-Charset: utf-8

--FormBoundaryXYZ
Content-Disposition: form-data; name="image"; filename="filename.png"
Content-Type: image/png

IMAGE_DATA
--FormBoundaryXYZ--

它是中等API
我已经尝试过了。

var axios = require("axios")

var data = (await axios("https://example.com/image.png")).data;

axios.post("https://api.medium.com/v1/images",{image: data},{
headers: {
 "Content-Type" : "multipart/form-data",
 "Authorization" : "Bearer " + process.env.key
}
}).then(x=>console.log(x.data))

然后我得到以下错误。

Error: Request failed with status code 400

它使用Medium API上传图像,我想获取一个远程图像,并将其转换为多部分/表单数据并通过API上传,HTTP请求似乎令人困惑,我想要等效的axios代码,有人请帮助吗?

sxpgvts3

sxpgvts31#

是否尝试使用FormData而不是普通对象?
例子应该是这样

const formData = new FormData();
formData.append('image', data);
axios.post('https://api.medium.com/v1/images', formData, {
  headers: {
    "Content-Type" : "multipart/form-data",
    "Authorization" : "Bearer " + process.env.key
  }
});

参考:How to post a file from a form with Axios

相关问题