使用Guzzle发送带有JSON的POST请求 [英] Using Guzzle to send POST request with JSON

查看:2304
本文介绍了使用Guzzle发送带有JSON的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$client = new Client();
$url = 'api-url';

$request = $client->post($url, [
    'headers' => ['Content-Type' => 'application/json'],
    'json' => ['token' => 'foo']
]);

return $request;

然后我拿回502 Bad Gateway,并将资源解释为Document,但以MIME类型application/json传输

And I get back 502 Bad Gateway and Resource interpreted as Document but transferred with MIME type application/json

我需要使用json发出POST请求.如何在Laravel中用Guzzle做到这一点?

I need to make a POST request with some json. How can I do that with Guzzle in Laravel?

推荐答案

尝试一下

$response = $client->post('http://api.example.com', [
    'json' => [
       'key' => 'value'
     ]
]);

dd($response->getBody()->getContents());

这篇关于使用Guzzle发送带有JSON的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆