1
votes

Am working on an a Laravel application whereby am posting some data to an API using Guzzle Http Client. The APIhas passport authentication which requires the token of the authenticated user to be passed on the headers. The headers also accept application/json as content-type and accept type.

I am also passing the data via POST request

The problem is that I keep getting a null response.

 public
 function post_policies($data, $token, $url) {

     $client = new Client();
     $serverURL = 'http://localhost/digital-apps-apis/public'.
     '/'.$url;

     $body = $client - > request('POST', $serverURL, $data, [
         'Accept' => 'application/json',
         'Content-Type' => 'application/json',
         'Authorization' => 'Bearer '.$token,
     ]) - > getBody();

     $contents = $body - > getbody() - > getContents();
     $data = json_decode($contents);
     dd($data);

 }
1

1 Answers

0
votes
    $body = $client->request('POST', $serverURL , $data, [
        'debug' => true,  //switch it to false before go live
         'headers' => [        
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $token, 
    ]]
)->getBody();

You must define your headers in the headers array.

Since it's a post i don't see you actually sending any data in the body. But if you do use the form_params array exactly like i used the headers array.