Using Guzzle to send POST request with JSON

13,613

Solution 1

Give it a try

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

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

Solution 2

you can also try this solution. that is working on my end. I am using Laravel 5.7.

This is an easy solution of Make a POST Request from PHP With Guzzle

   function callThirdPartyPostAPI( $url,$postField  )
   {
     $client = new Client();
     $response = $client->post($url , [
        //'debug' => TRUE,
        'form_params' => $postField,
        'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
        ]
     ]);
    return $body = $response->getBody();
  }

For Use this method

    $query['schoolCode'] =$req->schoolCode;
    $query['token']=rand(19999,99999);
    $query['cid'] =$req->cid;
    $query['examId'] =$req->examId;
    $query['userId'] =$req->userId;

    $tURL = "https://www.XXXXXXXXXX/tabulation/update";

    $response = callThirdPartyPostAPI($tURL,$query);

    if(  json_decode($response,true)['status'] )
    {
        return success(["data"=>json_decode($response,true)['data']]);
    }

Solution 3

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();
Share:
13,613

Related videos on Youtube

Kira
Author by

Kira

Updated on June 04, 2022

Comments

  • Kira
    Kira almost 2 years
    $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?

  • Morteza Rajabi
    Morteza Rajabi over 6 years
    @KiraArik which version of Guzzle are you using?
  • Amit
    Amit about 5 years
    It should be $request in dd() not $result. and a square bracket is missing. $request = $client->post('api.example.com', [ 'json' => [ 'key' => 'value' ] ]); dd($request->getBody()->getContents());
  • TPHughes
    TPHughes about 3 years
    The question asks how to send a request with JSON. This example will not work.
  • pankaj kumar
    pankaj kumar about 3 years
    can you share error code that you are getting?
  • pankaj kumar
    pankaj kumar about 3 years
    can you share error that you are getting … because in my server it is working..
  • pankaj
    pankaj about 3 years
    no , both methods working in my project.. i was facing same issue in my project many time. i used CURL for this .. but thank Pankaj ji...
  • pankaj
    pankaj about 3 years
    please help me , i need some technical support please call me 9031261290