Viewed   239 times

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

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

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

 Answers

4

For Guzzle <= 4:

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

$request = $this->client->post(
    $url,
    [
        'content-type' => 'application/json'
    ],
);
$request->setBody($data); #set body!
$response = $request->send();
Tuesday, November 29, 2022
1

You have to specify stream and sink options like this:

$res = $client->request('GET', 'http://localhost:3002/', [
    'stream' => true,
    'sink' => STDOUT, // Default output stream.
    'on_headers' => ...
]);

After these additions you will be able to stream responses chunk by chunk, without any additional code to copy from response body stream to STDOUT (with echo).

But usually you don't want to do this, because you will need to have one process of PHP (php-fpm or Apache's mod_php) for each active client.

If you just want to serve secret files, try to use an "internal redirect": through X-Accel-Redirect header for nginx or X-Sendfile for Apache. You will get the same behavior, but with less resource usage (because of high optimized event loop in case of nginx). For configuration details you can read an official documentation or, of course, other SO questions (like this one).

Monday, August 22, 2022
3

You have to use headers config sections for headers, not the root level.

return new Client([
    'base_uri' => env('API_HOST'),
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer ' . $token,
        'Content-Type' => 'application/json',
    ],
]);
Thursday, August 18, 2022
 
5

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>() {
           @Override
           public void onResponse(JSONObject response) {
               try {
                   //Process os success response
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               VolleyLog.e("Error: ", error.getMessage());
           }
       });

// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(request_json);

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>() {
        @Override
        public void onResponse(String response) {
            Log.i("LOG_VOLLEY", response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("LOG_VOLLEY", error.toString());
        }
    }) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

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

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

                responseString = String.valueOf(response.statusCode);

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

    requestQueue.add(stringRequest);
} catch (JSONException e) {
    e.printStackTrace();
}
Friday, August 12, 2022
 
asdcvf
 
3

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','/','google.com',80,0,0,86400,0);
$ua->cookie_jar($cookies);
# 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 www.google.com:80 (Bad hostname 'www.google.com')

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'], 'http://proxyhost.my.domain.com:8080/');

Monday, August 15, 2022
 
qtgye
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :