Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Guzzle to send POST request with JSON

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

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

return $request;

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

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

like image 475
Kira Avatar asked Dec 11 '25 14:12

Kira


2 Answers

Give it a try

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

dd($response->getBody()->getContents());
like image 109
Morteza Rajabi Avatar answered Dec 14 '25 06:12

Morteza Rajabi


Take a look..

$client = new Client();

$url = 'api-url';

$headers = array('Content-Type: application/json');

$data = array('json' => array('token' => 'foo'));

$request = new Request("POST", $url, $headers, json_encode($data));

$response = $client->send($request, ['timeout' => 10]);

$data = $response->getBody()->getContents();
like image 41
rAm Avatar answered Dec 14 '25 05:12

rAm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!