I've two working project communicating between each other via an API built in a Laravel. So far there is only simple POST requests made with GuzzleHttp 6.
And I am currently trying to have a new POST request made from 1 to 2, which would send a couple of simple fields along with one file.
Project 1 has a form, on the form submit I handle the data and want to send them to project 2 via a POST request to this new API endpoint.
I've tried different guzzle options 'multipart', 'form_data' etc and realised they may not be combined together. Now I understood that this options are exclusive and using only "multipart" seems the way to go.
But when I send my request to Laravel no data nor file are there.
Here is the code for my request
$options = [
'multipart' =>
[
[
'name' => 'data',
'contents' => '{"field_1":"Test","field_2":"Test","field_3":"Test"}',
'headers' =>
[
'Content-Type' => 'application/json',
],
],
[
'name' => 'file',
'filename' => 'test.pdf',
'Mime-Type' => 'application/pdf',
'contents' => file_get_contents($_FILEs['text_file']['temp_name']),
]
]
];
$this->client->request('POST', "api/test_post", $options)
I also gve this a try:
$options = [
'multipart' =>
[
[
'name' => 'field_1',
'contents' => 'Test',
],
[
'name' => 'field_2',
'contents' => 'Test',
],
[
'name' => 'file',
'filename' => 'test.pdf',
'Mime-Type' => 'application/pdf',
'contents' => fopen($_FILEs['text_file']['temp_name'],'r'),
]
]
];
$this->client->request('POST', "api/test_post", $options)
If I look the request content on the receiving end, nothing is there. No field or file.
I've seen couples posts, some say to include headers some say not too. I kinda got lost and amd now running out of ideas.
I would expecet the infos to be as if they where form post I guess:
$request->inpust('field_1') -> 'test'
$request->inpust('field_2') -> 'test'
$request->inpust('field_3') -> 'test'
$request->file('file') -> my uploaded file
Also I should point out that I am not exactly sure how multipart/form-data works, so that might not help me.
If you can point me to the right direction, that would help a lot
Well I finally figured it out. The second example from above is the way to go also be sure to check the headers of the request and the client...
As this API has been running for quite some time and was only doing json type requests, the Client was instantiated with
$options = [
headers => [ 'Content-Type' => 'application/json']
]
Which, as stated in multiple answers across the internet, prevents Guzzle to automatically set the Content-Type depending of the request options.
In my case, removing this line made Guzzle enable to set it properly when provided with 'multipart' option.
Also, as all other requests are using the 'json' options, Guzzle also works it's magic and set 'Content-Type' => 'application/json' as well.
Related
maybe someone has experienced the same problem, I want to make a GET request by sending data in the url parameter,
the endpoint need request like this:
https://test.com/sales/transaction/?page=1&pageSize=1&transactionDateFrom=2023-01-24T00:00:00.000Z&transactionDateTo=2023-01-24T15:59:59.000Z
but if i send from guzzle using dynamics data, the request is like this:
https://test.com/sales/transaction/?page=1&pageSize=5&transactionDateFrom=2023-01-2500%3A00%3A00.000Z&transactionDateTo=2023-01-2523%3A59%3A59.000Z
my code to send request:
$req = $client->request('GET','https://test.com/sales/transaction/', [
'query' => [
'page' => 1,
'pageSize' => 5,
'transactionDateFrom' => 2023-01-24T00:00:00.000Z,
'transactionDateTo' => 2023-01-24T15:59:59.000Z
],
'headers' => $headers
]);
how to resolve the timezone format to normal string like this
transactionDateFrom=2023-01-24T00:00:00.000Z&transactionDateTo=2023-01-24T15:59:59.000Z
maybe someone has solved this problem before and can provide some information, thanks
I am connected to my Google sheets, when I run a post request for https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create and set no properties, it creates a new spreadsheet.
I would love to add a title so I did this
$options = [
'form_params' => [
'properties' => [
'title' => 'A new title'
]
]
];
$client->post('v4/spreadsheets', $options);
I am using Guzzle to make this call. As a response I'm getting HTTP status code 400 (Bad Request) when adding title to form params.
How can I solve this?
You really should be using Google's SDK for this since they already have support for one in PHP.
According to their docs, sheet properties are set in a Google_Service_Sheets_SpreadsheetProperties instance which takes the title property.
An example from their docs:
<?php
$spreadsheet = new Google_Service_Sheets_Spreadsheet([
'properties' => [
'title' => $title
]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
'fields' => 'spreadsheetId'
]);
printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId);
This is the example adobe have in their documentation.
I have tried with Guzzle:
$client->request('POST', 'transientDocuments', [
'multipart' => [
[
'name' => 'test',
'contents' => fopen('pdfs/test.pdf', 'r'),
'filename' => 'file.pdf',
'headers' => [
'Content-Type' => 'multipart/form-data',
'Content-Transfer-Encoding' => "binary",
]
],
]
]);
fopen returns resource(13) of type (stream)
But every time I get {"code":"NO_FILE_CONTENT","message":"Must provide file body"}.
You should write code to satisfy the following request shown in image below.
Explanation
When u log in with your e-sign account you will be redirected to one of the hosts (like api.in1.echosign.com). You need to put File as form-data parameter key and your file as value. You get Mime-Type based on file extension. In response you will get TransientDocumentId which can be used for the creating and sending agreements etc.
Was stuck with the same but realised that the parameters name and filename to be enclosed in double quotes.
'name' => '"File"',
'filename' => '"file.pdf"',
PS. my code is in .Net, not php
I am using elasticsearch 6.2 with elasticsearch-php 6.0 client. There is situation i got stuck. I need to update field userid = 987 where userid = 123. I went through update API, Here in every query we need to pass document ID in API (like POST test/_doc/1/_update). First i need to fetch _id and then i have to make update query with POST test/_doc/{_id}/_update
It won't possible to every time produce _id. It didn't help me.
I found another option to use _update_by_query. In which i got success by using following API:
curl -XPOST 'localhost:9200/my_index/my_type/_update_by_query?pretty' -H 'Content-Type: application/json' -d '
{
"query":{
"term":{
"userid":123
}
},
"script":{
"lang":"painless",
"inline":"ctx._source.userid = params.value",
"params":{
"value":987
}
}
}'
I am not finding any reference which shows how i can use _update_by_query with elasticserach-php client. Plus let me know if you guyz have better way to tackle this. Thanks!
I got solution So i would like to share:
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
$update = [
'index' => 'my_index',
'type' => 'my_type',
'conflicts' => 'proceed',
'body' => [
'query' => [
'term' => [
"userid" => 987
]
],
'script' => [
'lang' => 'painless',
'inline' => 'ctx._source.userid = params.userid',
'params' => [
'userid' => 123
]
]
]
];
$results = $client->updateByQuery($update);
It solve my problem. I think elasticserach should document this.
I have a fresh installation of elasticsearch 5.0.0 and elasticsearch-php . I am trying to create an index.
I run the code from this index management documentation:
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index'
];
// Create the index
$response = $client->indices()->create($params);
and it works. I create an index successfully.
I try the next code snippet:
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'my_type' => [
'_source' => [
'enabled' => true
],
'properties' => [
'first_name' => [
'type' => 'string',
'analyzer' => 'standard'
],
'age' => [
'type' => 'integer'
]
]
]
]
]
];
// Create the index with mappings and settings now
$response = $client->indices()->create($params);
and I get:
Elasticsearch\Common\Exceptions\BadRequest400Exception with message 'No handler found for uri [/my_index] and method [POST]'
any ideas why?
This code used to work when I used elasticsearch 2.0
EDIT: I found this question so either it is a problem with elasticsearch-php or I need to update it I guess
I am using elasticquent which I have just realized requires elasticsearch-php version <2.2 so this is what is causing the problem
Looking at the error message:
No handler found for uri [/my_index] and method [POST]
This means that your create index call is using an HTTP POST method under the hood. In previous versions (i.e. pre 5.0), the elasticsearch-php client used to create indices with an HTTP POST but since ES 5.0 only HTTP PUT is accepted to create a new index.
This change back in september made this create call compatible with ES 5.0 again.
The only possible explanation is that you have ES 5.0 installed but you don't have the 5.0 version of the elasticsearch-php client installed.
Since you're running Elasticquent which doesn't yet support ES 5, you can temporarily go around this issue by modifying the Endpoints/Indices/Create.getMethod() method to always return PUT instead of POST and your call will work again, but you might run into other incompatibilities.