I am using Ixudra/Curl in my laravel project.
I have an issue when I try to upload a file to a Java API.
With Curl Using command line is working file with:
curl -X POST \
http://<<api url>>' \
-H 'Content-Type: multipart/form-data' \
-H 'cache-control: no-cache' \
-H 'content-type: multipart/form-data; boundary=xxxxx\
-F 'data={"tables" : ... (no need to post all data)' \
-F 'file=#C:\Users\UserName\Downloads\Test.xls'
With PHP code I am trying without success:
Curl::to('<<api url>>')
->withContentType('multipart/form-data; boundary=' . hash('sha256', uniqid('', true)))
->withData(array('data' => {"tables" : ... ))
->withFile('file', 'C:\Users\UserName\Downloads\Test.xls', 'application/vnd.ms-excel', 'Test.xls')
->post();
The response I am getting with this code is:
"exception":"org.springframework.web.multipart.support.MissingServletRequestPartException","message":"Required request part 'file' is not present"
Any idea? Thanks for your time
Problem solved,
Seems this library didn't like that I specify the content-type.
Once I deleted that line, everything worked as expected
Related
I am trying to troubleshoot a symfony's HTTP client request. How can one display the anticipated request as a cURL request such as the following? Thank you
curl -X 'PUT' \
'https://example.com/projects/01GPX8HPBQ54SA11JKWPWFESM6/document_acl' \
-H 'accept: application/ld+json' \
-H 'Authorization: Bearer blaBlaBla' \
-H 'Content-Type: application/ld+json' \
-d '{}'
I want to add product with image in database and add to cart in shopware 6 using API.
I was stuck at upload image when add product. product added successfully but not media image.
I create custom plugin and upload image through that.
Thank you in advance.
A new media entity needs to be created before they can be referenced for a add product api call. I've added a example to create a media and assign it to a product for you, might need to be tweaked to your specifications:
Create Empty Media Entity
curl --request POST \
--url https://127.0.0.1:8000/api/media \
--header 'Authorization: Bearer OAUTH2_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
"id": "eb85c29df15c41e8abd79552dce73e9a"
}'
The uuid needs to be unique and unused in shopware.
Upload Media Content
curl --request POST \
--url 'https://127.0.0.1:8000/api/_action/media/eb85c29df15c41e8abd79552dce73e9a/upload?extension=png&fileName=test' \
--header 'Authorization: Bearer OAUTH2_TOKEN' \
--header 'Content-Type: image/png' \
--data 'base64encode(file_get_contents($file))'
The image needs to be base64 encoded and can then be transfered as a request body.
Assign media to product
curl --request PATCH \
--url https://127.0.0.1:8000/api/product/2a88d9b59d474c7e869d8071649be43c \
--header 'Authorization: Bearer OAUTH2_TOKEN' \
--header 'Content-Type: application/json' \
--data '{
"coverId": "eb85c29df15c41e8abd79552dce73e9a"
}'
The media can also added via the sync endpoint and/or used as a gallery element.
I have an Nginx and PHP container running in docker. When I run the following request, $_POST is empty.
I use the Phpstorm option "Break at the first line in PHP scripts" to Debug.
curl -X POST 'localhost/api/v1/customers' -H 'Content-Type: application/json' --data-raw '{"id":"12345"}'
What can cause that the $_POST is empty? Is there a config in Nginx required?
When I try the request with form data, it works but I need that it works with JSON.
curl -X POST 'localhost/api/v1/customers' -H 'Content-Type: application/x-www-form-urlencoded' --data-urlencode 'id=12345'
When posting application/json data, $_POST is always empty and you can obtain the JSON request body with PHP:
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
$ curl -X POST 'localhost/api/v1/customers' \
-H 'Content-Type: application/json' \
-d '{"id":"12345"}'
Array
(
[id] => 12345
)
I have a cURL call in a PHP 7.2 script running on CentOS 7 that is passing empty data to an endpoint. In diagnosing the issue, I have tried replicating the issue using cURL on the command-line, and not using the PHP code. Here's the command I am running (I was told to send a Content-Length of 0):
/usr/bin/curl -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Content-Length: 0" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' "https://url.here/endpoint_debug.php"
In the endpoint script I am printing the input stream.
$data = file_get_contents('php://input');
print "HEADERS:\n\n";
print_r(apache_request_headers()) . "\n\n";
print "RAW Data:\n\n";
print "'" . $data . "'\n\n";
No matter what I have done, the $data variable is always empty. The result I see is always ''. I also tried sending it to https://webhook.site/ and it, too, shows that the "Form values" are empty.
I have tried with -X POST and without. I tried changing the order of the calls so the URL is before the data (-d) switch. I have tried --data as well.
What could be the issue? I have replicated this on 2 different servers calling the same endpoint. Other code in the same project handles file_get_contents('php://input') properly, so I don't think that's it.
Any guidance would be appreciated!
TLDR: You need to remove -H "Content-Length: 0"
It doesn't make sense why you were instructed to send this if you want a response.
I tried your command unmodified with a webhook.site url, and it can return the content body (for example) without sending the head, and return nothing if sending the header:
Quite simply, this command:
/usr/bin/curl -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' 'https://webhook.site/27f6128e-2f82-4157-99da-99464f22122f'
Returns the body. Whereas this command:
/usr/bin/curl -H "Content-Length: 0" -H "App-Key: 321321321313" -H "App-Token: 321321321312" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"reportCustomFields":[{"label":"THIS","value":"THAT","shown":true},{"label":"UP","value":"DOWN","shown":true}]}' 'https://webhook.site/27f6128e-2f82-4157-99da-99464f22122f'
Obviously returns nothing
The server response headers indicate as much :
> Accept: application/json
> Content-Type: application/json
> Content-Length: 0
>
* upload completely sent off: 112 out of 112 bytes
< HTTP/1.1 200 OK
< Server: nginx/1.14.2
I'm using PHP shell_exec to call the Bluemix Text to Speech API and my code is working well except when I call the text with SSML tags like my second code.
How could I use text and SSML in the same data query?
$result = shell_exec("/usr/bin/curl -k -u 'XXX':'XXX' -X POST \
--header 'Content-Type: application/json' \
--header 'Accept: audio/wav' \
--max-time 90000 \
--output 'public/uploads/audios/padrao_bomdia.wav' \
--data '{\"text\":\"Hello! This is an test.\", \"voice\":\"pt-BR_IsabelaVoice\"}' \
'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=pt-BR_IsabelaVoice'");
$result = shell_exec("/usr/bin/curl -k -u 'XXX':'XXX' -X POST \
--header 'Content-Type: application/json' \
--header 'Accept: audio/wav' \
--max-time 90000 \
--output 'public/uploads/audios/padrao_bomdia.wav' \
--data '{\"text\":\"Hello! <say-as interpret-as="letters">Hello</say-as> This is an test.\", \"voice\":\"pt-BR_IsabelaVoice\"}' \
'https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=pt-BR_IsabelaVoice'");
Does say-as interpret-as="letters" Hello fragment need the escape slashes around 'letters'?
Unfortunately, Only US-English Allison voice supports Expressive SSML. Isabela, the PT-BR voice, does not support SSML. That's why it's not working.
Reference:
Currently, the service supports expressiveness only for the US English
Allison voice (en-US_AllisonVoice). Using the element with any other
voice returns an error.