Skip to content
Advertisement

Proper syntax for Guzzlehttp request containing body and header

I have the following Guzzle send request on my controller :-

$client = new GuzzleHttpClient(['base_uri' => 'https://domainname/api/v1/']);

$response = $client->request('POST', 'user/register', [
   'headers' => [
       'Authorization' => Session::get('token'),
       'Content-Type'  => 'application/x-www-form-urlencoded'
    ]],
    [ 'form_params' => [
        'user_id' => $user->id,
        'start_date' => $start_date,
        'expiry_date' => $expiry_date
    ],
 ]);

 $response= $response->getBody();

The above syntax only sending the header but , what is the proper syntax i should use to send both header and body.

Advertisement

Answer

Guzzle docs has plenty of examples, try reading them.

$response = $client->request('POST', $url, [ 
    'headers' => [
         'User-Agent' => 'testing/1.0',
         'Accept' => 'application/json', 
         'X-Foo' => ['Bar', 'Baz']
    ],
    'form_params' => [
         'field_name' => 'abc', 
         'other_field' => '123', 
         'nested_field' => [
             'nested' => 'hello' 
         ] 
    ] 
]);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement