GET Request with JSON Body?

10,646 views
Skip to first unread message

James Carr

unread,
Dec 4, 2012, 12:21:33 PM12/4/12
to guz...@googlegroups.com
Does anyone have an example of making a GET request with a json body?

Also, is there any mechanism that will convert arrays used for a request into a json packet?


Thanks!
James

Michael

unread,
Dec 4, 2012, 7:11:22 PM12/4/12
to guz...@googlegroups.com
A GET request that sends a body? That is not supported in Guzzle and probably isn't supported by the server you're interacting with. GET requests should not contain bodies.

Are you talking about sending a GET request that gets a JSON response? Check out http://guzzlephp.org/tour/http.html#json-responses

> Also, is there any mechanism that will convert arrays used for a request into a json packet?

This is possible using a service description, but is not part of the Guzzle\Http layer. You'll need to just json_encode() the array before setting it as the body of a request.

-Michael

James Carr

unread,
Dec 5, 2012, 9:06:02 AM12/5/12
to guz...@googlegroups.com
Well, specifically I was wanting to use guzzle with elasticsearch, which uses GET requests with a body: http://www.elasticsearch.org/guide/reference/api/search/

thewilkybarkid

unread,
Dec 9, 2012, 2:23:32 PM12/9/12
to guz...@googlegroups.com
The bottom of http://www.elasticsearch.org/guide/reference/api/search/request-body.html says that you can also do a POST.

Chris

Random Guy

unread,
May 29, 2014, 6:37:10 AM5/29/14
to guz...@googlegroups.com
Hi all,

Sorry for bringing back an old thread.

I would be keen to know if there is a change from the Guzzle perspective when it comes to sending a JSON'ed body via a GET request through Guzzle.


Cheers.

Michael

unread,
Jun 1, 2014, 3:18:18 PM6/1/14
to guz...@googlegroups.com
What exactly would you like to do? Do you want to send a body with a GET request, or are you trying to send JSON data in the query string?

Guzzle 4 allows you to send a body with a GET request if you really want to, but I would highly discourage it. Simply pass the "json" request option with JSON data when sending a GET request. http://docs.guzzlephp.org/en/latest/clients.html#json. You should probably not do this as most servers will not download the body and your request will time out.

As for sending JSON data in the query string: no, Guzzle will never support this out of the box because there's no clear way that this would work. You can do this yourself by simple setting one of the query string parameters to your JSON encoded data, and Guzzle will url encode the data as needed.

Random Guy

unread,
Jun 3, 2014, 10:04:46 AM6/3/14
to guz...@googlegroups.com
Hey Michael, thanks for the reply.

I am implementing an internal client to test our REST API, and part of it involves to send a GET request with JSON data to the corresponding URI using two params.

IE

  $imageResponse = $this->client->get('rest/user/current/resources/fetch', [
                                         'auth' => [self::USERNAME, self::PASSWORD ],
                                         'body' => [
                                                         'skeletonResource='.json_encode($skeletonServer),
                                                         'fetchParameters='.json_encode($fetchParameters)
                                                    ]
                                                  
                                          ]);

This fails at the moment.

Michael Dowling

unread,
Jun 3, 2014, 11:40:22 AM6/3/14
to guz...@googlegroups.com
What is the error? I should mention that I don't want to add code specifically to support this as sending a body with a GET request is not typically supported by clients , servers, or proxies. You should change the HTTP method of that API call I think.
--
You received this message because you are subscribed to the Google Groups "Guzzle - PHP HTTP client and REST client framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to guzzle+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Random Guy

unread,
Jun 4, 2014, 5:45:45 AM6/4/14
to guz...@googlegroups.com
Well, it is pretty hard -from my perspective- to amend the HTTP method, as I am just an employee. :)

Thanks alot!
Reply all
Reply to author
Forward
0 new messages