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,
])
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' => ''
]
]
]);
i use Fast Excel library from https://github.com/rap2hpoutre/fast-excel, can i put return from ->download('file.xlsx') to Guzzle post request? with this code i'm not get anything
$httpClient = new \GuzzleHttp\Client([
'headers' => [
'Authorization' => mdmTokenGenerate($token),
'Accept' => 'application/json'
],
'verify' => false
]);
$response = $httpClient->post(
"{$client->domain}/api/push", [
'multipart' => [
[
'name' => 'file',
'contents' => (new FastExcel(MasterNumbers::limit(1000)->get()))->download('file.xlsx')
]
]
]
);
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,
]
]);
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?
I'd like to add some data to a Guzzle Http Request. There are file name, file content and header with authorization key.
$this->request = $this->client->request('POST', 'url', [
'multipart' => [
'name' => 'image_file',
'contents' => fopen('http://localhost:8000/vendor/l5-swagger/images/logo_small.png', 'r'),
'headers' =>
['Authorization' => 'Bearer uCMvsgyuYm0idmedWFVUx8DXsN8QzYQj82XDkUTw']
]]);
but I get error
Catchable Fatal Error: Argument 2 passed to GuzzleHttp\Psr7\MultipartStream::addElement() must be of the type array, string
given, called in vendor\guzzlehttp\psr7\src\MultipartStream.php on line 70 and defined in vendor\guzzlehttp\psr7\src\MultipartStream.php line 79
In Guzzle 6 documentation is something like this: http://docs.guzzlephp.org/en/latest/request-options.html#multipart
Who knows where I made a mistake?
Here is the solution. Header with access token should be outside multipart section.
$this->request = $this->client->request('POST', 'request_url', [
'headers' => [
'Authorization' => 'Bearer access_token'
],
'multipart' => [
[
'Content-type' => 'multipart/form-data',
'name' => 'image_file',
'contents' => fopen('image_file_url', 'r')
]
]
]);
As per the docs, "The value of multipart is an array of associative arrays", so you need to nest one level deeper:
$this->request = $this->client->request('POST', 'url', [
'multipart' => [
[
'name' => 'image_file',
'contents' => fopen('http://localhost:8000/vendor/l5-swagger/images/logo_small.png', 'r'),
'headers' => ['Authorization' => 'Bearer uCMvsgyuYm0idmedWFVUx8DXsN8QzYQj82XDkUTw']
]
]
]);
try this
works for me
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Utils;
$this->client = new Client([
'base_uri' => 'https://baseurl'
]);
$body = Utils::tryFopen($tempPath . $fileName, 'r');
$res = $this->client->request(
'POST',
'url',
[
'headers' => [
...
],
'body' => $body
]
);