When i try to send a http request it return me a status 400 (Bad request , invalid headers)
the API require an access token and Content Type:application/x-www-form-urlencoded
this is my code :
the access token is very long
please help me
$response = Http::contentType('application/x-www-form-urlencoded')
->withToken($access_token)
->timeout('2000')
->get('https://example /.../...',[
]);`
use withHeaders() ref link https://laravel.com/docs/8.x/http-client#headers
$response = Http::withHeaders(["Content-Type"=>"application/x-www-form-urlencoded"])
->withToken($access_token)
->timeout('2000')
->get('https://example /.../...',[
]);`
Related
I make a Guzzle 6 request, and this request responses with a 401.
$client = new Client();
$response = $client->request('GET', ....
....
My Script will stop then and will return a error message.
GuzzleHttp \ Exception \ ClientException (401)
Client error: GET https://.....?lang=de resulted in a 401 Unauthorized response: Unauthorized
Try catch does not work.
How can I intercept the error message?
Thanks for help!
I found the error.
I have added the parameter
'http_errors' => false
http://docs.guzzlephp.org/en/stable/request-options.html#http-errors
Now I can check the response status:
if ($response->getStatusCode() != 200) {
echo "error";
}
We need more code to help you, which parameters you use, what is your request, etc ...
You can try to send your request with Postman to check if your parameter are correcly sended
I have been asked to write a program that takes a list of numbers and sends a post to ms.4url.eu via JSON/HTTP Post in format:
{
"username":"a",
"password":"b",
"msisdn":"071231231234",
"webhook":"http://example.com"
}
it receives a JSON Response,
{
"status":"ok",
"id":"1234-1234-12344423-123123"
}
I have been told I can use ngrok for the webhook and I have to send a HTTP Response 200 within 1s.
I should receive a Webhook Response:
{
"id":"1234-1234-12344423-123123",
"msisdn":"071231231234",
"status":"unavaliable",
"error":"1b",
"errorDesc":"Abscent Subscriber"
}
How would I go about grabbing the data from the JSON response and Responding with a HTTP 200 in order to receive the second response with the data?
I can get the first response in curl but I am unable to get the webhook working to a php file using ngrok and HTTP response sent to request the main information in the second response.
Edited :
I have executed the curl command,
curl -H 'content-type: application/json' \
-d '{"username":"a", "password":"b", "msisdn":"07123123124","webhook":"http://example.com/"}' \
HTTPS://ms.4url.eu/lookup
of which I get the first response "status ok". I would like to know how to get the response(Json format) in php using http post to the URL and the using a webhook to respond with 1second with a http 200 response to receive the further information from the API URL.
I ended up using ngrok and viewing the Raw POST response and getting the JSON and viewing the Raw data I still had more code to do in order t make this question valid as there are too many points to answer.
I want to get information about the request I have sent, such as url, body sent etc. I am using the Async feature which uses promises (example below)
$client = new \GuzzleHttp\Client();
return new \GuzzleHttp\Psr7\Request\Request('POST', $this->getUrl(), $this->getHeaders(), $this->getBody());
Is there a way where I can get request information from the promise or from the response?
Reason I am asking this is because I need to store some information about the request in the database later on, which can not be done before I sent the request.
What I tried so far is
Getting the information from the promise with the following methods
$promise->getRequest()
$pomise->Request
$promise->request
$promise->getHandlers()
Thank you
When you initialize a new Request then you have to send it. It's not sent by default. A request is sent when a Client calls send method on it. When request is completed you have access to all information about response:
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Client;
$client = new Client();
$request = new Request('GET', 'https://www.google.com');
$response = $client->send($request);
$response->getHeaders(); // array of all retreived headers
$response->getStatusCode(); // http status code
$response->getReasonPhrase(); // http status phrase
and you initialized a wrong Request object, Guzzle is not shipped with \GuzzleHttp\Psr7\Request\Request but \GuzzleHttp\Psr7\Request.
Now with a correct way of sending a request, getting request information is as simple as below:
print_r($request->getHeaders()); // array of all sent headers
echo $request->getUri(); // requested URI
cURL works fine:
curl -H "X-ApiToken: myapitoken" https://api.fulcrumapp.com/api/v2/records
Guzzle does not:
$client = new Client();
$request = $client->createRequest('GET', "https://api.fulcrumapp.com/api/v2/records");
$request->setHeader("X-ApiToken:" , "myapitoken");
$response = $client->send($request);
This responds with a 401 error: not authorized. This is my first time using Guzzle but in my searches I haven't seen this error. Seems like a simple request so I'm not sure why it is failing.
Thank you!
I had to add another header to explicitly tell it to handle json
$request->setHeader("Accept" , "application/json");
Thank you for pointing out that the 401 was a false error - the real error was the 406, which made me read how to actually fix that.
The setHeader method takes the header name without the colon. Change "X-ApiToken:" to "X-ApiToken".
I am getting a 401 on Guzzle 4.2 and the same setup works on Postman. Code below.
// Create a client with a base URL
$client = new GuzzleHttp\Client(['base_url' => 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1']);
// Send a request to https://github.com/notifications
$response = $client->get();
//Auth
$response->addHeader('Authorization', "auth-code");
//send
$r = $response->send();
dd($r->json());
The error is:
GuzzleHttp \ Exception \ ClientException (401)
Client error response [url] cloud.feedly.com/v3/streams/contents?streamId=user/user-id/global.all&count=1 [status code] 401 [reason phrase] Unauthorized
Looking at the documentation page as per this line:
$response = $client->get();
It will send a get request, with no authorisation, hence the 401 response.
Try the following instead
// Create a client with a base URL.
$client = new GuzzleHttp\Client();
$request = $client-> createRequest('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1');
$request->setHeader('Authorization', "auth-code");
// Send.
$response = $client->send($request);
dd($response->json());
The above creates a request, set the authorisation header on it. Then once prepared it actually sends it.
I think it worked in Postman because your headers are set, if you remove the authorization header it will likely fail there too.
I have not tested this but think it will work.
$response = $client->get('GET', 'cloud.feedly.com/v3/streams/contents?streamId=user/user-id/category/global.all&count=1', ['auth' => ['YOUR_USERNAME', 'YOUR_PASSWORD']]);
The 401 error means you should authenticate yourself with a valid username and password
Regards