【问题标题】:REST API Post Error: The body of the request is not valid JSONREST API 发布错误:请求的正文不是有效的 JSON
【发布时间】:2024-01-24 11:06:01
【问题描述】:

我正在通过 axios 从 react 客户端发出 post 请求。发布请求是微软自定义翻译器 api。由于某种原因,我不断收到 400 错误。

当我检查 chrome 的网络选项卡下的错误响应时,我看到了这个错误 => {"code":400074,"message":"The body of the request is not valid JSON."}

这个帖子请求与邮递员完美配合。我在这里错过了什么?

let config = {
            headers: {
                'Content-Type': 'application/json',
                'Ocp-Apim-Subscription-Key': '<valid-key>',
                
            },
            params: {
                'api-version': '3.0',
                'to': 'de',
                'category': '<valid-category-id>'
            }
        }

        let data = {
            "body" : [
                {"Text": "Hello"}
            ]
        }

        axios.post('https://api.cognitive.microsofttranslator.com/translate/', data, config)
        .then((res) => console.log(res))
        .catch((err) => console.log(err));

【问题讨论】:

    标签: json reactjs post axios microsoft-translator


    【解决方案1】:

    在我去掉“body”键后它起作用了。

    let data =  [
                    {"Text": "Hello"}
                ]
    

    【讨论】:

      【解决方案2】:

      发布请求通常没有尾部正斜杠。尝试删除最后一个正斜杠,这样你的 url 就变成了:

      'https://api.cognitive.microsofttranslator.com/translate'
      

      【讨论】:

      • 即使没有尾部正斜杠,我也看到了相同的结果。
      最近更新 更多