I'm sure this is going to be marked as a duplicate, but I've looked through all the given questions on the same subject and tried many of the suggested solutions, and they haven't worked.
I'm in a laravel project and I have a post request going out through guzzle.
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $url, [
'headers' => [
'Authorization' => 'Bearer ' . $apiToken,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'allow_redirects' => false,
// 'allow_redirects'=>['strict'=>true]
],
'json' => json_decode($logText, true)
]);
I keep on getting the response back "message": "The GET method is not supported for this route. Supported methods: POST."
I've checked and indeed, it's sending a GET request to the same $url specified above.
I didn't have those allow_redirects settings at first, but both settings were offered up as potential solutions when I googled around. Unfortunately, both options result in the same error message: The GET method is not supported for this route.
Why in the world is my POST request changing to a GET request?
I've also tried $client->post instead, and THAT became a GET request as well.
I've also double checked that the GET error message isn't actually coming from inside the POST request: it's not. The POST route isn't being hit at all.
PHP version 7.2, Laravel version 6.0.2, Guzzle version 6.5.3
Check for redirects on the server, such as HTTP -> HTTPS. Redirects are always a GET request, which will mess up non-GET routing. Using the correct protocol all the way through (such as always use HTTPS) will bypass the redirect.
Related
I am trying to post some data using a Guzzle Client Http request in Laravel. But for some reason the server respons with the message that it can't find the property Id in the JSON object, while the property Id is clearly in the request. The Guzzle documentation says that assigning the array to a json property in the request will result in a Json object being sent.
$url = "https://blablabla.com/api";
$key = "1234";
$data = [
'Id' => "4"
];
$response = Http::withHeaders([
"Authorization" => $key
])->post($url, [
'json' => $data
]);
Now I have tested the api in Postman and don't experience any problems. I even use the same api in a different php application using curl and it works perfect. So obviously there is something wrong with lines of code above and not with the api. I have tried different things but nothing works.. I have a feeling that the solution is so simple.. but for the last 6 hours I couldn't figure it out.. So please help before I go crazy :)
Laravel documentation on page HTTP Client says: "By default, data will be sent using the application/json content type". So you don't need to use 'json' property in post data. Just sent it directly: ->post($url, $data).
I am running into a weird issue -> I've got a controller set up to send a POST request to another application using GuzzleHttp - which works fine when this request is started from our VueJs client.
Now I am developing an Artisan Command (per request by the customer) to simplify calling this endpoint like 50+ times (as it generates videos) -> to call this endpoint I am using the following snippet as I am calling an internal controller:
$request = Request::create(
route('videos.new', [], false),
'POST',
[
// Some daata
]
);
$response = app()->handle($request);
$result = json_decode($response->getContent());
But the issue is, now the exact same code in that controller sends a GET request instead of POST to the other application and I cannot figure out why as the method is HARD CODED in there.
I know it's a GET request as I am logging all requests entering the other application at the moment, url etc all looks correct, except that it's a GET request now
Request send with:
$cdnReq = new \GuzzleHttp\Psr7\Request(
'POST',
"/generate/$type?" . http_build_query($query),
[
'Content-Type' => 'application/json'
],
json_encode($input)
);
$this->beforeRequest($cdnReq);
// Step 03: Send request to cdn
Log::debug("Request(" . $cdnReq->getMethod() . ") will be send to: " . $client->getConfig('base_uri') . $cdnReq->getUri(), $input);
$cdnRes = $client->send($cdnReq);
Does anyone have any idea why this is happening?
INFO:
Laravel Version: Laravel/Framework 6.18.32
The issue was related to the URL being called as HTTP and redirected to HTTPS, which changes the POST to GET.
I'm trying to use Elasticsearch-php version 5.0 to send search queries to Elasticsearch 6.4.2.
One of the breaking changes from 5.0 to 6.0 is that there is the "strict content type validation"
which means that requests to Elasticsearch must sent with "Content-type: application/json" header.
In order to add this header, I tried to use polyfractal's suggestion from this thread:
$params = [
'index' => $index,
'type' => $mapping,
'body' => $query,
'client' => [
'curl' => [CURLOPT_HTTPHEADER => array('Content-type: text/plain')]
]
];
$res = $this->mESClient->search($params); // this is Elasticsearch/Client
return $res;
but for some reason, I keep getting "Notice: Array to string conversion" when the code tries to do curl_setopt_array(), and the request is net sent.
Please note: that when I remove the 'client' part of the $params array the request is being received in the Elasticsearch.
According to Version Matrix, you should use elasticsearch-php 6.0 when dealing with ES >=6.
elasticsearch-php 5.0 is not compatible with ElasticSearch 6.
The thread you mentioned, relates to ES-PHP 1.x/2.x, which may have different syntax for options. It's not relevant for your situation, except that one of comments says the same that I just did above.
FYI, if you're using Elasticsearch 6.0+, you need to upgrade your ES-PHP client to the 6.0 branch too. ES-PHP 6.0+ sets the content-type headers automatically: fd3b0f1
Found the problem. It seems that there is a buggy functionality when trying to set HTTP headers using curl and specifying authorization details in the URL.
for example:
https://username:password#host:port
For some reason the client copies the curl http headers into other curl options (that requires string and not array) and, therefore, the exception of Array to string conversion thrown from curl_setopt_array.
When I removed the authorization details from the host URL and used the curl authorization header everything worked.
I'm trying to get some data from an endpoint. And the endpoint allows only requests that originate from one specific domain (which is not mine)
Is it possible to make a request with Guzzle and make it "pretend" as if it was coming from the allowed origin?
Currently I am trying to set some headers to achieve that and get the response back but it is always returning me code 200 with content-length: 0
You can set Origin header to what you want as long as you are making a request out of browser's control. In Guzzle it could be set like this:
$client->request('GET', '/data', [
'headers' => [
'Origin' => 'http://foo.bar',
]
]);
If targeting host is satisfied with this only header then you are fine otherwise you won't get your expected response.
at the moment I am fighting with a http post request within cakePHP.
I am using the code below to communicate with an external API that requires Authorization.
The problem is, as soon as I add the body with the JSON context, the whole application is hanging. It's loading forever in the browser, not coming back with any results. Have to stop and start Apache to come back to normal. Tested on different machines, same effect.
If I don't send the (JSON) body, I am getting obviously a 500 server error, but it's telling me that the authorization is working otherwise I would get an unauthorized.
If I do a "GET" request to the same API, it's working fine and coming back with a result.
The JSON syntax is clean, if I process the variable and the other information using an API tester it's working (https://apigee.com/console).
UPDATE: It is working if the request is valid. However, on the API tester it's giving me a "400 Bad Request". I would need to handle that within my application but in this case the situation described above is happening. Expectation would be that it's returning the error.
$options = array(
'body' => $json_order,
'header' => array(
'Authorization' =>'123456',
'Content-Type' => 'application/json; charset=utf-8',
),
);
$result=$HttpSocket->post('https://<address>',null,$options);
debug($HttpSocket->request);
same effect if I would do it the other way
$options = array(
'header' => array(
'Authorization' =>'123456',
'Content-Type' => 'application/json; charset=utf-8',
),
);
$result=$HttpSocket->post('https://<address>',$json_order,$options);
debug($HttpSocket->request);
Any help would be more than appreciated!
To clear that up: The problem was on the far end not responding correctly with an error code. =