I have the following code that I use whenever I want to make POST requests using Guzzle:
$request = $client->request('POST', $url, [
'form_params' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
The code works without any issues and the information within $params is always sent, however when I change the request type from POST to PUT so that the request becomes:
$request = $client->request('PUT', $url, [
'form_params' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
The request suddenly stops sending the data contained within $params.
I have tested the endpoint the request is send to with Insomnia with both POST and PUT requests and both types are processed as expected, so I am certain the issue is not there.
What can be causing the data from Guzzle to be send using the POST method but not when using the PUT?
This behaviour described in guzzle documentation form-params
form_params - Used to send an application/x-www-form-urlencoded POST request.
Probably, you enough pass the parameters in json format:
$request = $client->request('PUT', $url, [
'json' => $params,
'headers' => [
'Referer' => '(intentionally removed)',
'Accept' => 'application/json',
]
]);
Related
I am creating a user with the api provided. I am using Laravel and trying to store data to smartrmail and docs to create new subscriber is here https://docs.smartrmail.com/en/articles/636615-list-subscribers
Each time i send request i get following error:
Server error: `POST https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers` resulted in a `500 Internal Server Error` response: {"error":"param is missing or the value is empty: subscribers"}
{"error":"param is missing or the value is empty: subscribers"}
I am using Laravel and my code is here
Route::get('smartrmail',function(){
$headers = [
'Accept' => 'application/json',
'Authorization' => 'token f91715d5-3aac-4db3-a133-4b3a9493a9a4',
'Content-Type' => 'application/json',
];
$client = new GuzzleHttp\Client([
'headers' => $headers
]);
$data = [
"subscribers"=>[
[
"email"=> "vanhalen#example.com",
"first_name"=> "van",
"last_name"=> "halen",
"subscribed"=> true,
]
]
];
$res = $client->request('POST', 'https://go.smartrmail.com/api/v1/lists/1sptso/list_subscribers', [
'form_params' => [
$data
]
]);
return($res);
// echo $res->getStatusCode();
});
Anybody help me to figure out what is wrong here. I am following this docs
https://docs.smartrmail.com/en/articles/636615-list-subscribers
to create a new subscriber
Instead of
'form_params' => [
$data
]
use
'json' => $data
Explanation
You want to send json data (I assume that because you set header 'Content-Type' => 'application/json', which means that you are sending json), but form_params is used for application/x-www-form-urlencoded.
json sets header to application/json and sends data as json.
As you set proper header, this should work too:
'body' => $data
Proper name of param you can find in Guzzle docs, I used uploading data part.
Does anybody know the correct way to PUT using Guzzle? my code is not working
but my post methods are working
$enrolment = $client->request('PUT', $url,[
'form_params' => [
'contactID' =>12345,
'type' =>'w'
],
'headers' => [
'apitoken' => $api_token,
'wstoken' => $ws_token
]
]);
resulted in a 500 Internal Server Error response:↵{"DATA":"","ERROR":true,"MESSAGES":"key [TYPE] doesn't exist","CODE":"0","DETAILS":""}
The PUT request does't accept form_params type as request option, so it may ignore the setting.
From Docs:
form_params
Used to send an application/x-www-form-urlencoded
POST request.
Maybe you can try using json for PUT request.
In the json part of Docs, it uses PUT as well.
$enrolment = $client->request('PUT', $url,[
'json' => [
'contactID' =>12345,
'type' =>'w'
],
'headers' => [
'apitoken' => $api_token,
'wstoken' => $ws_token
]
]);
I am trying to make a post on laravel using Guzzle. I already got one post working but that one doesn't have a header that I need. The problem is when I try to include that same header and raw JSON body I always get the error: MethodNotAllowedHttpException. From my understanding this is the post request function with some structural error (just my thinking).
My code is the following:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'x-auth-token' => $token,
],
'body' => $body
]);
The code above is returning the error.
The following code (from a different function with different goals) just doesn't have the 'x-auth-token' header and it is working just fine:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => $body
]);
$token = $response->getHeader('X-Subject-Token')[0];
Update: The error that i get is: Expecting to find auth in request body. The server could not comply with the request sin.
i want to send a request per guzzle.
i try this but guzzle send no request.
$response = $client->request('POST', $uri, [
'auth' => [
'user',
'pw'
],
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded'
],
'body' => [
'token' => $accessToken
]
]
);
$response->getBody()->getContents()
if i delete the body part. the request is working. but i need the body too. and i get no response. is there a possibility to see what guzzle exactly is sending?
You should use form_params instead. body takes a raw byte sequence.
In my Laravel application, I periodically need to POST data to an API using Guzzle.
The API users a bearer token to authenticate, and requests and accepts raw json. To test, I accessed the API using Postman, and everything worked wonderfully.
Postman Headers:
Accept:application/json
Authorization:Bearer [token]
Content-Type:application/json
And Postman Body:
{
"request1" : "123456789",
"request2" : "2468",
"request3" : "987654321",
"name" : "John Doe"
}
Postman returns a 200, and a JSON object as a response.
Now, when I try the same with Guzzle, I get a 200 status code, but no JSON object gets returned. Here's my Guzzle implementation:
public function getClient($token)
{
return new Client([
'base_uri' => env('API_HOST'),
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
]);
}
$post = $client->request('POST', '/path/to/api', [
'json' => [
'request1' => 123456789,
'request2' => 2468,
'request3' => 987654321,
'name' => 'John Doe',
]
]);
Is there some trick to POSTing JSON with Guzzle? If not, is there a way to debug what's going on under the hood?
I cannot, for the life of me, understand what the difference is between the Postman POST and the Guzzle POST.
You have to use headers config sections for headers, not the root level.
return new Client([
'base_uri' => env('API_HOST'),
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
],
]);