我正在尝试使用 axios 从我的 express 应用程序调用另一个服务(也尝试过 node-fetch)
因此,当我使用 curl、soapui 或 swagger 调用该服务时,它可以工作。
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '"02"' 'http://example.com/api/v1/something'
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用
axios.put('http://example.com/api/v1/something', '"03"')
.then((response) => {
res.sendStatus(200);
})
.catch((error) => {
console.log(error);
res.sendStatus(500);
});
Run Code Online (Sandbox Code Playgroud)
我收到“请求失败,状态码为 415”
通过在标题中添加 Content-Type 来修复
axios({
method: 'PUT',
url,
data: JSON.stringify(req.body),
headers:{'Content-Type': 'application/json; charset=utf-8'}
})
.then((response) => {
res.sendStatus(200);
})
.catch((error) => {
logger.error(error);
res.status(500).send(error);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8552 次 |
| 最近记录: |