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?
Related
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' => ''
]
]
]);
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',
],
]),
]);
I'm trying to consume this PayPal API: https://developer.paypal.com/docs/api/orders/v2/#orders_create
Here is my PHP code:
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Content-Type' => 'application/x-www-form-urlencoded',
'Authorization' => "Bearer " . $this->token
],
'form_params' => [
"intent" => "CAPTURE",
"purchase_units" => [
"amount" => [
"currency_code" => "USD",
"value" => "100.00"
]
]
]
]
);
Error received:
Client error: POST https://api.sandbox.paypal.com/v2/checkout/orders
resulted in a 415 Unsupported Media Type response:
{"name":"UNSUPPORTED_MEDIA_TYPE","message":"The request payload is not
supported","debug_id":"6bd372e5171ee","details":[ (truncated...)
'Content-Type' => 'application/x-www-form-urlencoded',
This media type is not accepted, only application/json
See the documentation for how to send json: http://docs.guzzlephp.org/en/stable/request-options.html#json
Alternatively, use the Checkout-PHP-SDK, with some guide documentation here: https://developer.paypal.com/docs/checkout/reference/server-integration/
For the best user experience, create two routes on your server, one for 'Set Up Transaction' and one for 'Capture Transaction', to be used from the following front-end code: https://developer.paypal.com/demo/checkout/#/pattern/server
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'),
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.