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

第三方GET API需传请求体,Axios不支持该如何解决?

解决GET请求携带请求体的问题

Axios 实现方式

Axios默认会丢弃GET请求的data参数,但可以通过axios.request()方法绕开这个限制,强制发送请求体。直接指定请求方法为GET,并传入data字段即可:

axios.request({
  method: 'GET',
  url: 'https://第三方API地址',
  data: {
    // 这里放请求体数据
    key1: 'value1',
    key2: 'value2'
  },
  headers: {
    'Content-Type': 'application/json' // 根据API要求设置对应的Content-Type
  }
})
.then(response => {
  console.log(response.data);
})
.catch(error => {
  console.error(error);
});

如果上面的方法在某些环境下不生效,可以自定义transformRequest来确保请求体被保留:

axios({
  method: 'GET',
  url: 'https://第三方API地址',
  data: { key: 'value' },
  transformRequest: [(data, headers) => {
    // 直接返回序列化后的data,不做额外处理
    return JSON.stringify(data);
  }],
  headers: {
    'Content-Type': 'application/json'
  }
});

原生Fetch 实现方式

如果Axios的方案还是有问题,可以用原生Fetch API,它对GET请求带请求体的支持更直接:

fetch('https://第三方API地址', {
  method: 'GET',
  body: JSON.stringify({ key1: 'value1', key2: 'value2' }),
  headers: {
    'Content-Type': 'application/json'
  }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));

注意事项

虽然HTTP规范并没有禁止GET请求携带请求体,但部分服务器或代理可能会拒绝这类请求。不过既然你用PHP Curl能成功,说明目标服务器是支持的,上面的方法应该可以正常工作。

内容的提问来源于stack exchange,提问作者Sabith

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 08:10:22