Home > front end >  Sending GET Request with a JSON body
Sending GET Request with a JSON body

Time:07-19

I am trying to consume an API that allows JSON bodies to be sent to a GET endpoint, however I cannot find a way to send a body to a GET endpoint.

I have tried:

Http::withBody(json_encode([
     'field' => 'value',
]), 'application/json')
->get('foo.bar/auth/user');

Which sends no body.

And:

Http::asJson()
->get('foo.bar/auth/user', [
     'field' => 'value',
]);

Which turns the body into query params.

How can I achieve this?

Note: Its not under my control whether the API is correct by allowing JSON bodies with a GET endpoint.

CodePudding user response:

You can call the send method, which is what get ends up calling to send the request:

Http::asJson()->send($method, $url, ['json' => $data]);
  • Related