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' => ''
]
]
]);
Related
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'm using Laravel 6.2. I have a named route
Route::get('/dummy/{id}', 'Api\V1\DummyDataController#show')->name('dummy_data_show');
I cannot write the test for it, I get the error Illuminate\Routing\Exceptions\UrlGenerationException: Missing required parameters for [Route: dummy_data_show] [URI: api/v1/dummy/{id}].
These are my attempts (only relevant code):
$request = $this->withHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
]
)->json('GET',
Route('dummy_data_show'),
[
'id' => 1,
]
);
and also
$request = $this->withHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
]
)->json('GET',
Route('dummy_data_show'),
1
);
Of course if I try with
$request = $this->withHeaders(
[
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$token,
]
)->json('api/v1/dummy/1');
I don't get the error. What is my error? Thank you!
You are not passing any parameters to your route method. The parameters should be inside the parenthesis.
Change:
Route('dummy_data_show'),
[
'id' => 1,
]
To:
route('dummy_data_show', [
'id' => 1,
])
I'm trying to use variables in php to add values to the json on Guzzle, but that appear that's not working, i'm doing it wrong? Thanks in advance, im new in the use of Guzzle. (Guzzle 6)
$send_response = $send->post("https://yyy.com/services/" , [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' .$token
],
'json' => [
'key' => $value,
'key2' => $value2,
]
]);
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.
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?