1

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 Answer 1

0
    $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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.