Guzzle: handle 400 bad request
As written in Guzzle official documentation: http://guzzle.readthedocs.org/en/latest/quickstart.html
A GuzzleHttp\Exception\ClientException is thrown for 400 level errors if the exceptions request option is set to true
For correct error handling I would use this code:
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
try {
$response = $client->get(YOUR_URL, [
'connect_timeout' => 10
]);
// Here the code for successful request
} catch (RequestException $e) {
// Catch all 4XX errors
// To catch exactly error 400 use
if ($e->hasResponse()){
if ($e->getResponse()->getStatusCode() == '400') {
echo "Got response 400";
}
}
// You can check for whatever error status code you need
} catch (\Exception $e) {
// There was another exception.
}
GuzzleHttp\Client 400 bad request on Laravel 5.5
Try the below code:
$client = new \GuzzleHttp\Client(['headers' => ['Content-Type' => 'application/json',
'apikey'=> config('app._api_key'),
'debug' => true
]
]);
$URI = 'http://api.example.com';
$body['sender']='Test_sender';
$body['recipient']=config('app.test_recipient');
$body['message_body']='Test body';
$body=json_encode($body);
$URI_Response = $client->request('POST',$URI,['body'=>$body]);
$URI_Response =json_decode($URI_Response->getBody(), true);
return $URI_Response;
Note: I would suggest you to handle error please refer GuzzleDocumentation
Guzzle POST request always returns 400 Bad Request
If you're using Guzzle 6 (and you probably should be), you're actually constructing in a more complex way than you need to, such that the endpoint is not receiving the expected JSON. Try this instead:
$client = new Client([
'base_uri' => 'https://my.endpoint.com/api',
'headers' => [
'Accept' => 'application/json',
...other headers...
]
]);
$data = [...your big slab of data...];
$response = $client->post('/kitely/path', ['json' => $data]);
// a string containing the results, which will depend on the endpoint
// the Accept header says we will accept json if it is available
// then we can use json_decode on the result
$result = $response->getBody()->getContents();
Guzzle 400 Bad Request
You are sending two different requests. With JavaScript, you send the data as JSON in the request body. With Guzzle however, you're using form-params, which is a different format.
To fix this, just replace form-params
key with json
and remove "content-type"=>"application/x-www-form-urlencoded"
.
Guzzle Post Using API Key Keeps Resulting in 400 Bad Request
I was able to get this working. It looks like the ActiveMQ setup was expecting a JSON message when I'm trying to send an XML message.
I was able to determine this by turning on the Guzzle debug flag.
$client = new GuzzleHttp\Client([
'debug' => true
]);
Then adding the content-type to the headers.
'headers' => array(
'apikey' => 'apikeyhere',
'Content-Type' => 'application/xml',
)
Hope this helps someone in a similar boat.
Guzzle authentication returning 400 error
Working with:
$client = new \GuzzleHttp\Client();
$url = "";
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/x-www-form-urlencoded',
'Authorization' => 'Basic $token'
],
'form_params' => [
'grant_type' => 'password',
'scope' => 'forintegration',
'username' => '',
'password' => ''
]
]);
$body = $response->getBody();
print_r(json_decode((string) $body));
GuzzleHttp - 400 error exception but with curl working perfectly
You need
'json' => $body
instead of
'form_params' => $body
Related Topics
Laravel 5.2 Not Reading Env File
Executing a PHP Script with a Cron Job
In PHP, How to Extract Multiple E-Mail Addresses from a Block of Text and Put Them into an Array
Symfony2: After Successful Login Event, Perform Set of Actions
Php: Best Way to Check If Input Is a Valid Number
Why Is It Whenever I Use Scandir() I Receive Periods at the Beginning of the Array
How to Decode This JSON String
If Singletons Are Bad Then Why Is a Service Container Good
How to Get File Url Using Storage Facade in Laravel 5
How to Get Greenwich Mean Time in PHP
When *Not* to Use Prepared Statements
How to Get Final Url After Following Http Redirections in Pure PHP