Search code examples
laravelhttprequestguzzlelaravel-7

Laravel 7 HTTP Client - Unable to send POST request with `body`


Using Laravel 7.6 and it's built-in HTTP Client.

I'm trying to send a simple POST request with body in Raw JSON format to my other domain but no luck:

$response = Http::post('https://example.com', [
    'body' => '{ test: 1 }'
]);

I get 400 Bad Request - Client error - because my server expects body as a mandatory.

What am I missing here?


Solution

  • $response = Http:post('http://example.com/', [
       'foo' =>   'bar'
    ]);
    

    Or this

    $response = Http::withHeaders([
     'User-Agent' => 'Some Agent',
    ])->asForm()->post('http://www.example.com/', [
      'foo' => 'bar',
    ]);