DrewT DrewT - 3 months ago 11
PHP Question

Can't POST boolean values in Guzzle 6 HTTP Client

I am really frustrated at the moment and could use the aid of someone more experienced with Guzzle.

I am connecting to an API which requires a boolean value if I want a code message response from the API server instead of a full link response.

Here are examples of the difference between the two return types as accessed by a raw http client (Postman):

Boolean code enabled:
enter image description here

Boolean code disabled:
enter image description here

The problem I'm having is that when I make the same request using Guzzle 6, I will always get the full link response and can never get the boolean value in the post body to be applied. It seems the boolean parameter is getting stringified as 'true' (this is my guess).

Therefore the following two POST requests produce exactly the same result:

// define request parameters
$this->req_body = [
'email' => $encrypted_email,
'code' => true
];

// request url
$request_url = $api_endpoint . self::RETURN_TYPE;

// instance of Guzzle Client
$client = $this
->client();

// abstract connection
// XXX: this request needs no headers
$response = $client
->post($request_url, array(
'form_params' => $this->req_body
));
// real data
$data = $response
->getBody()
->getContents();

// send output
$this->response = $data;


And if I try with the
code
form parameter commented out:

// define request parameters
$this->req_body = [
'email' => $encrypted_email\\,
//'code' => true
];

// request url
$request_url = $api_endpoint . self::RETURN_TYPE;

// instance of Guzzle Client
$client = $this
->client();

// abstract connection
// XXX: this request needs no headers
$response = $client
->post($request_url, array(
'form_params' => $this->req_body
));
// real data
$data = $response
->getBody()
->getContents();

// send output
$this->response = $data;


The returned API response is always:
"{"success":{"code":200,"message":"https:\/\/webservices.bvdpetroleum.com\/users\/user-password-reset\/q8VqSAbfTOkW0EMvSTfK5qSS4zr28rSwdQy3D\/uc9wtz3+RI4LH7hDkh\/ZbTfqcC"}}"


How do I send
Boolean
values in the Guzzle 6
form_params
array? Any insight would be greatly appreciated because I don't want to have to switch to
CURL
.

Thanks!

Answer

Just change the form_params array to send as a json post body :)

$response = $client->post($request_url, array(
               'json' => $this->req_body
            ));