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

如何向GET接口发送JSON请求体?

解决Laravel Http客户端向GET接口发送JSON请求体的问题

Laravel的Http::get方法会自动将数组参数转为查询字符串,且默认封装逻辑会忽略GET请求的请求体设置,所以你之前的两种方式都无法达到预期效果。要实现需求,可以直接使用send方法手动构建请求:

方法一:直接通过send方法配置请求

$response = Http::send('GET', 'foo.bar/auth/user', [
    'headers' => [
        'Content-Type' => 'application/json',
    ],
    'body' => json_encode([
        'field' => 'value',
    ]),
]);

方法二:结合withBody与send方法

$response = Http::withBody(
    json_encode(['field' => 'value']),
    'application/json'
)->send('GET', 'foo.bar/auth/user');

这两种方式都绕开了Http::get的默认参数处理逻辑,强制为GET请求带上JSON格式的请求体,适配目标API的特殊要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 03:24:22