【问题标题】:axios PUT call not working getting 415axios PUT 调用无法正常工作,得到 415
【发布时间】:2017-09-07 04:00:28
【问题描述】:

我正在尝试使用 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'

但是当我尝试使用

axios.put('http://example.com/api/v1/something', '"03"')    
        .then((response) => {
             res.sendStatus(200);
        })
        .catch((error) => {
             console.log(error);
             res.sendStatus(500);
    });

我收到“请求失败,状态码 415”

【问题讨论】:

  • 这就是我的想法,所以我检查了 jsonlint.com 上的“03”是否是有效的 JSON。

标签: express axios


【解决方案1】:

通过在标题中添加 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);
            });

【讨论】:

    猜你喜欢
    • 2022-07-14
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 2022-01-02
    • 1970-01-01
    • 2016-12-30
    • 2018-11-07
    相关资源
    最近更新 更多