GuzzleHttp POST Request Data Changed Unexpectedly - php

As per the title, the data that the endpoint receiving is somehow manipulated over the request.
Before submitting the request, I have this array of objects as my data. Notice the length.
So I assume that I will be receiving this data from the endpoint.
And yes, I did. But it is somehow changed. Compare this image to the first one.
What could be done wrong? Here's my guzzle code
$response = $client->request(
'POST',
env( 'APP_URL' ) . 'api/migrate',
[
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => 'application/json',
],
'form_params' => [
'access_token' => $access_token,
'data' => $data
]
]
);
The $data is the first screenshot.

Related

Create an asset programmatically Azure Media Setrvice v3 PHP

I'm trying to create an asset with the Guzzle client using this code
$response = $this->httpClient->request('PUT', $request_url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->getAccessToken(),
],
'form_params' => [],
]);
I'm getting this error
400 Bad Request` response: { "error": { "code": "InvalidResource", "message": "The input resource must be specified in the request bod (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).
I was able to create assets with postman using the same credentials and tokens.
The error message seems to point to the missing storage account name in the body.
https://learn.microsoft.com/en-us/rest/api/media/assets/create-or-update?tabs=HTTP#create-an-asset
Can you check if body is being sent correctly.
I won't delete the question so while searching I found a solution you need to send a body like this in order to create an asset.
$response = $this->httpClient->request('PUT', $request_url, [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->getAccessToken(),
],
'json' => [
'properties' => [
'description' => ''
]
]
]);

Is there a way to prevent Guzzle from appending [] to field names with multiple values in a POST request?

When using Guzzle to POST a field with multiple values, brackets get appended to the field names:
<?php
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://www.example.com/test',
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
);
$client->request('POST', '', [
'form_params' => [
'foo' => [
'hello',
'world',
],
],
]);
Guzzle sends this data as foo[0]=hello&foo[1]=world. Is there a way to omit the brackets, so that the data is sent as foo=hello&foo=world? Google Forms, for example, returns a 400 error response if the brackets are included.
There currently is no way to achieve this by using the automatic encoding with post_params, so you'll have to provide your own raw POST body if you need this exact format.
Fortunately, there's a very helpful function in GuzzleHttp\Psr7\Query (which should automatically come installed if you required guzzle through composer), named build, which does exactly what you need.
use GuzzleHttp\Psr7\Query;
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://www.example.com/test',
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
]
]);
$client->request('POST', '', [
'body' => Query::build([
'foo' => [
'hello',
'world',
],
]),
]);

send url in form params guzzle http

I use laravel and GuzzleHttp for send post request same as following
$client = new Client(['verify' => false]);
$data = [
'headers' => [
'Authorization' => 'code',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form-params' => [
'redirect_uri' => 'http://localhost',
'code' => 'MZTADnFF6m'
]
];
$response = $client->request('POST', 'https://oom.com', $data);
but my problem is 'http://localhost' send as 'http:\/\/localhost' to api server
how can i fix this problem?
You can use stripslashes() for achieve this. You can try
stripslashes("http:\/\/localhost");
it will resultd in
http://localhost
as a result
use urlencode
here it is
'redirect_uri' => urlencode('http://localhost'),

Guzzle Post Request with Query Params and Body

I want to use guzzle to send a POST request to a WebService. This Request MUST send 2 parameters on the query string and a body with form_params.
Example: http://localhost:5000/api/V1/Clients/AddClient?sitecode=PT&username=test
And then add a body with form_params to this request.
I'm trying with this code:
$Client = new \GuzzleHttp\Client([ //Instantiate the guzzle client
'headers' => [
'Content-Type' => 'application/json',
]
]);
$response = $Client->post("http://localhost:5000/api/V1/Clients/AddCustomer", [
'headers' => [
'Authorization' => 'Bearer '.$token
],
'query' => [
'sitecode' => "PT",
'username' => "Test",
],
'form_params' => [
'Client' => json_encode($ClientRegister),
]
]);
All of this in PostMan works:
The code returns a 500 internal server error. Is expected to get a 200 OK.
Can anyone help me?

PHP GuzzleHttp . How to send a json post?

I have this post:
{"latitude":"","longitude":"","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"normal","checkStoreAvailability":false}
And i'm trying to send it like this on Guzzle 6.0+
'headers' => [
'Content-Type' => 'application/json',
'Referer' => 'https://www.rituals.com/es-es/stores'],
'body' => '{"latitude":"","longitude":"","countryCode":"ES","filterPostalCode":"","filterCity":"","filterCountryCode":"ES","searchText":"","nextPageToken":0,"storeType":"normal","checkStoreAvailability":false}']
But it's not working, any way to send everything without formating it like I posted? thanks!
First of create Client object
$client = new Client([
'http_errors' => false,
'verify' => false,
]);
And then your request with params
$response = $client->request($requestMethod, $url, array_merge(
['json' => $body],
['headers' => $headers]
));

Categories