guzzlepsr-7

Guzzle not sending PSR-7 POST body correctly


It is either not being sent, or not being received correctly. Using curl direct from the command line (using the -d option) or from PHP (using CURLOPT_POSTFIELDS) does work.

I start with a PSR-7 request:

$request = GuzzleHttp\Psr7\Request('POST', $url);

I add authentication header, which authenticates against the API correctly:

$request = $request->withHeader('Authorization', 'Bearer ' . $accessToken);

Then I add the request body:

// The parameter for the API function
$body = \GuzzleHttp\Psr7\stream_for('args=dot');
$request = $request->withBody($body);

I can send the message to the API:

$client = new \GuzzleHttp\Client();
$response = $client->send($request, ['timeout' => 2]);

The response I get back indicates that the "args" parameter was simply not seen by the API. I have tried moving the authentication token to the args:

'args=dot&access_token=123456789'

This should work, and does work with curl from the command line (-d access_token=123456789) but the API fails to see that parameter also when sending cia curl (6.x) as above.

I can see the message does contain the body:

var_dump((string)$request->getBody());
// string(8) "args=dot"
// The "=" is NOT URL-encoded in any way.

So what could be going wrong here? Are the parameters not being sent, or are they being sent in the wrong format (maybe '=' is being encoded?), or is perhaps the wrong content-type being used? It is difficult to see what is being sent "on the wire" when using Guzzle, since the HTTP message is formatted and sent many layer deep.

Edit: Calling up a local test script instead of the remote API, I get this raw message detail:

POST
CONNECTION: close
CONTENT-LENGTH: 62
HOST: acadweb.co.uk
USER-AGENT: GuzzleHttp/6.1.1 curl/7.19.7 PHP/5.5.9

args=dot&access_token=5e09d638965288937dfa0ca36366c9f8a44d4f3e

So it looks like the body is being sent, so I guess something else is missing to tell the remote API how to interpret that body.

Edit: the command-line curl that does work, sent to the same test script, gives me two additional header fields in the request:

CONTENT-TYPE: application/x-www-form-urlencoded
ACCEPT: */*

I'm going to guess it is the content-type header which is missing from the Guzzle request which is the source of the problem. So is this a Guzzle bug? Should it not always sent a Content-Type, based on the assumptions it makes that are listed in the documentation?


Solution

  • The GuzzleHttp\Client provides all necessary wrapping.

    $response = $client->post(
        $uri,
        [
            'auth' => [null, 'Bearer ' . $token],
            'form_params' => $parameters,
    ]);
    

    Documentation available Guzzle Request Options

    Edit: However, if your requests are being used within GuzzleHttp\Pool then, you can simply everything into the following:

    $request = new GuzzleHttp\Psr7\Request(
        'POST',
        $uri,
        [
           'Authorization' => 'Bearer ' . $token,
           'Content-Type' => 'application/x-www-form-urlencoded'
    
        ],
        http_build_query($form_params, null, '&')
    );