Asked  7 Months ago    Answers:  5   Viewed   49 times

Does anybody know the correct way to post JSON using Guzzle?

$request = $this->client->post(self::URL_REGISTER,array(
                'content-type' => 'application/json'

I get an internal server error response from the server. It works using Chrome Postman.



For Guzzle <= 4:

It's a raw post request so putting the JSON in the body solved the problem

$request = $this->client->post(
        'content-type' => 'application/json'
$request->setBody($data); #set body!
$response = $request->send();
Wednesday, March 31, 2021
answered 7 Months ago

Guzzle implements PSR-7. That means that it will by default store the body of a message in a Stream that uses PHP temp streams. To retrieve all the data, you can use casting operator:

$contents = (string) $response->getBody();

You can also do it with

$contents = $response->getBody()->getContents();

The difference between the two approaches is that getContents returns the remaining contents, so that a second call returns nothing unless you seek the position of the stream with rewind or seek .

$stream = $response->getBody();
$contents = $stream->getContents(); // returns all the contents
$contents = $stream->getContents(); // empty string
$stream->rewind(); // Seek to the beginning
$contents = $stream->getContents(); // returns all the contents

Instead, usings PHP's string casting operations, it will reads all the data from the stream from the beginning until the end is reached.

$contents = (string) $response->getBody(); // returns all the contents
$contents = (string) $response->getBody(); // returns all the contents


Wednesday, March 31, 2021
answered 7 Months ago

If you could return it it wouldn't be async in code style. Return the promise and unwrap it on the outside.

function test() 
   $client = new GuzzleHttpClient();   
   $request = $client->createRequest('GET', 'http://l/?n=0', ['future' => true]);

   // note the return
   return $client->send($request)->then(function ($response) {
       //echo 'Got a response! ' . $response;
       return "n".$response->getBody();
     echo $body; // access body here inside `then`
Saturday, May 29, 2021
answered 5 Months ago

Usual way is to use a HashMap with Key-value pair as request parameters with Volley

Similar to the below example, you need to customize for your specific requirement.

Option 1:

final String URL = "URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "token_value");
params.put("login_id", "login_id_value");
params.put("UN", "username");
params.put("PW", "password");

JsonObjectRequest request_json = new JsonObjectRequest(URL, new JSONObject(params),
       new Response.Listener<JSONObject>() {
           public void onResponse(JSONObject response) {
               try {
                   //Process os success response
               } catch (JSONException e) {
       }, new Response.ErrorListener() {
           public void onErrorResponse(VolleyError error) {
               VolleyLog.e("Error: ", error.getMessage());

// add the request object to the queue to be executed

NOTE: A HashMap can have custom objects as value

Option 2:

Directly using JSON in request body

try {
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    String URL = "http://...";
    JSONObject jsonBody = new JSONObject();
    jsonBody.put("firstkey", "firstvalue");
    jsonBody.put("secondkey", "secondobject");
    final String mRequestBody = jsonBody.toString();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        public void onResponse(String response) {
            Log.i("LOG_VOLLEY", response);
    }, new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            Log.e("LOG_VOLLEY", error.toString());
    }) {
        public String getBodyContentType() {
            return "application/json; charset=utf-8";

        public byte[] getBody() throws AuthFailureError {
            try {
                return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
            } catch (UnsupportedEncodingException uee) {
      "Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                return null;

        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            String responseString = "";
            if (response != null) {

                responseString = String.valueOf(response.statusCode);

            return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));

} catch (JSONException e) {
Monday, July 19, 2021
answered 4 Months ago

As mentioned cookies are in HTTP::Cookies:

  • You need to create a cookie jar

  • You set the value of the cookies to put in the jar

  • You then associate that jar with your user agent

For example:

my $ua = LWP::UserAgent->new;
my $cookies = HTTP::Cookies->new();
$cookies->set_cookie(0,'cookiename', 'value','/','',80,0,0,86400,0);
# Now make your request

set_cookie has a rather large number of arguments:

set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, %rest )

This is because the cookie jar is designed from the point of view of a browser (a UserAgent), rather than a single request. This means not all the arguments are so important in this case.

The ones you need to get right are $key, $val, $path, $domain, $port.

Regarding the:

500 Can't connect to (Bad hostname '')

It means that LWP can't lookup the address for Google. Are you behind a web proxy? If so you will need to set your proxy in the UA too using something like:

$ua->proxy(['http', 'https'], '');

Wednesday, August 11, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :