Tuesday, 1 October 2019

Guzzle3 send raw Post request

I am working on project that uses Guzzle3 (v3.9.3), I would like to know how to send a raw post request, I have tried these solutions but none of them worked for me. found it in https://guzzle3.readthedocs.io/http-client/request.html?highlight=raw#raw-post-data

Solution :

$client = new Client();
$client->setDefaultOption('headers', array(
'Authorization' =>  'Bearer '.$token,
'Accept' => 'application/json'
));

$body = '{"filter_":{"user":{"email":"aaa@test.com"}}}';
$req = $client->post($url, array(), $body,
array(
 'cert' => array($certification, 'password'),
)
);
$response = json_decode($client->send($req)->getBody(true)); 

Solution 2 :

$client = new Client();
$client->setDefaultOption('headers', array(
 'Authorization' =>  'Bearer '.$token,
'Accept' => 'application/json'
));

$body = '{"filter_":{"user":{"email":"aaa@test.com"}}}';

$req = $client->post($url, array(), array(),
 array(
    'cert' => array($certification, 'password'),
 )
);

$req->setBody($body);
$response = json_decode($client->send($req)->getBody(true)); 

None of them worked for me,

Error : Client error response [status code] 404 [reason phrase] Not Found [url]

I have tried some solutions found in the internet (but for Guzzle6) it works but I don't get the right results (it doesn't take in consideration the filter that I have sent , which is the mail address, so I get all results)

...
$body = array(
'filter_' => array(
   'user' => array( "email" => $email )
 )
);

$req = $client->post($url, array(),array('body'=> $body),
array(
    'cert' => array($certification, 'password'),
)
);
...

On postman the call to WS work.

Thanks in advance



from Guzzle3 send raw Post request

No comments:

Post a Comment