You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Axios发送请求返回ERR_BAD_REQUEST的问题排查

解决Axios POST请求返回ERR_BAD_REQUEST的问题

以下是排查和解决方向:

  • 先获取服务端的具体错误信息
    你现在只打印了error,但Axios的错误对象里error.response包含了服务端返回的详细错误内容,修改catch代码:

    .catch((error) => {
      if (error.response) {
        console.log('服务端返回错误:', error.response.data);
        console.log('状态码:', error.response.status);
        console.log('响应头:', error.response.headers);
      }
    });
    

    这能直接看到服务端拒绝请求的具体原因,比如字段缺失、格式错误等,是最有效的排查方式。

  • 检查Axios请求的序列化方式
    虽然你设置了Content-Type: application/json,但可以尝试用axios.post的简写形式,避免config配置出错:

    axios.post('https://hereismyurl/v1/tokens/cards', payload, {
      headers: {
        "Accept": "application/json",
        "Content-Type": "application/json",
        'Authorization': 'Bearer pub_prod_some_public_key'
      }
    })
    .then(res => console.log(res.data))
    .catch(err => {
      if (err.response) console.log(err.response.data);
    });
    
  • 对比Postman的请求细节
    打开Postman的「Code」功能,选择「Axios」生成代码,和你自己的代码逐行对比:

    • 检查请求头的每个字段(包括大小写、空格)
    • 检查payload的结构、字段名、值的类型是否完全一致
    • 确认URL没有拼写错误(比如末尾是否有多余斜杠)
  • 排查是否有Axios拦截器干扰
    如果你的项目中配置了Axios拦截器,可能会修改请求的data或headers,暂时注释掉拦截器代码,再测试请求是否正常。

  • 检查payload的字段值类型
    比如installments是数字类型,你代码里是2(数字),确认Postman里也是传的数字而不是字符串,有些服务端对类型严格校验。

内容的提问来源于stack exchange,提问作者Carlos Daniel Ospina Salazar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 06:31:21