Send request to cURL with post data sourced from a file - http

I need to make a POST request via cURL from the command line. Data for this request is located in a file. I know that via PUT this could be done with the --upload-file option.
curl host:port/post-file -H "Content-Type: text/xml" --data "contents_of_file"

You're looking for the --data-binary argument:
curl -i -X POST host:port/post-file \
-H "Content-Type: text/xml" \
--data-binary "#path/to/file"
In the example above, -i prints out all the headers so that you can see what's going on, and -X POST makes it explicit that this is a post. Both of these can be safely omitted without changing the behaviour on the wire. The path to the file needs to be preceded by an # symbol, so curl knows to read from a file.

I need to make a POST request via Curl from the command line. Data for this request is located in a file...
All you need to do is have the --data argument start with a #:
curl -H "Content-Type: text/xml" --data "#path_of_file" host:port/post-file-path
For example, if you have the data in a file called stuff.xml then you would do something like:
curl -H "Content-Type: text/xml" --data "#stuff.xml" host:port/post-file-path
The stuff.xml filename can be replaced with a relative or full path to the file: #../xml/stuff.xml, #/var/tmp/stuff.xml, ...

If you are using form data to upload file,in which a parameter name must be specified , you can use:
curl -X POST -i -F "parametername=#filename" -F "additional_parm=param2" host:port/xxx

Most of answers are perfect here, but when I landed here for my particular problem, I have to upload binary file (XLSX spread sheet) using POST method, I see one thing missing, i.e. usually its not just file you load, you may have more form data elements, like comment to file or tags to file etc as was my case. Hence, I would like to add it here as it was my use case, so that it could help others.
curl -POST -F comment=mycomment -F file_type=XLSX -F file_data=#/your/path/to/file.XLSX http://yourhost.example.com/api/example_url

I was having a similar issue in passing the file as a param. Using -F allowed the file to be passed as form data, but the content type of the file was application/octet-stream. My endpoint was expecting text/csv.
You are able to set the MIME type of the file with the following syntax:
-F 'file=#path/to/file;type=<MIME_TYPE>
So the full cURL command would look like this for a CSV file:
curl -X POST -F 'file=#path/to/file.csv;type=text/csv' https://test.com
There is good documentation on this and other options here: https://catonmat.net/cookbooks/curl/make-post-request#post-form-data

I had to use a HTTP connection, because on HTTPS there is default file size limit.
https://techcommunity.microsoft.com/t5/IIS-Support-Blog/Solution-for-Request-Entity-Too-Large-error/ba-p/501134
curl -i -X 'POST' -F 'file=#/home/testeincremental.xlsx' 'http://example.com/upload.aspx?user=example&password=example123&type=XLSX'

Related

where to store request.json file

I am trying out Google video intelligent api by following the quick start guide here. I am using cloud shell to run all the codes. In the guide, to make an annotate video request, I need to create a request.json file.
Where to put this file once it's created?
Thanks very much for your help!
The request.json file in this case should be saved in the same directory that you'll make the curl request from in step 2 of that guide. The curl command points to that file at the end:
curl -s -H 'Content-Type: application/json' \
-H 'Authorization: Bearer ACCESS_TOKEN' \
'https://videointelligence.googleapis.com/v1beta1/videos:annotate' \
-d #request.json
The #request.json means it's looking in the current directory for the file.

-D- curl parameter

There is a sample in Jira API doc:
curl -D- -X GET -H "Authorization: Basic ZnJlZDpmcmVk" -H "Content-Type: application/json" "http://kelpie9:8081/rest/api/2/issue/QA-31"
What exactly does the -D- parameter mean? There is no description in curl documentation.
I'm also not sure whether -D- and -D mean the same thing.
It dumps headers to stdout.
The -D flag dumps headers to a file, and the following - instructs it to dump to stdout. From the linked spec:
-D, --dump-header
Write the protocol headers to the specified file.
It doesn't specify for this option, but this works like other options that take a filename:
Use "-" as filename to have the output sent to stdout.

write all the curl arguments in a separate file to execute multipart/form-data request on webservices

I need to write all the curl arguments in a separate file(myconfig.txt) to be executed and output for each different lines of command in output.txt, output1.txt, output2.txt...so on.
The webservices should be requested with post (multipart/form-data) method.
I am trying this command curl -K myconfig.txt -o output.txt
contents of
myconfig.txt include URL="http://1x2.2x1.x6.1x2:3000/latto/get_notifications"<option="to";mobile="+91999xx6xx3x"> into the output.txt - 'Cannot get /latto/get_notifications'. It seems like web-services are being requested as get method. Please anyone tell me the syntax to write in myconfig file. So that I can get the right output in file.
However, When I run this command sudo curl --form option="to" --form mobile="+9199999yyyxx" 1x2.2x1.x6.1x2:3000/latto/get_notifications, output is successfully printed in terminal.
When I do a curl -k config.txt with config.txt containing this:
URL="http://my.test.domain/index.php"
-d option="to"
-d mobile="+91999xx6xx3x"
I get the result:
POST
array(2) {
["option"]=>
string(4) ""to""
["mobile"]=>
string(15) "" 91999xx6xx3x""
}
So I don't known what you mean with <option ...> but when you put it into your config file as stated above, it should work.
Btw. here is the php script that answered:
<?php
echo $_SERVER['REQUEST_METHOD']."\n";
var_dump($_REQUEST);
?>
This webservice accepts request in POST method and enctype multipart/form-data
URL="http://my.test.domain/get_notifications"
-F option=to
-F mobile=+91999xx6xx3x
After a lot of hit and trial, I got this working: curl -k config.txt with config.txt containing the above.

how to post a json file with documents to couchdb

I am trying to post some documents to couchdb by curl and I have succeeded by choosing local file but not http-url... I have trying something like this:
curl -d #http://111.111.11.1/json/myjsonfile -X POST http://127.0.0.1:5984/MyTestDb/_bulk_docs -H "Content-Type: application/json"
I have been trying with many flags and tried many ways but I thing I am missing something. Is there anyone who can help?
The -d option for curl expects a local file only. You'll have to download it first. You could try piping the output of a curl download to a PUT to your CouchDB:
curl http://111.111.11.1/json/myjsonfile | curl -d #- -X PUT http://localhost:5984/MyTestDb....

CURL Command Line URL Parameters

I am trying to send a DELETE request with a url parameter using CURL. I am doing:
curl -H application/x-www-form-urlencoded -X DELETE http://localhost:5000/locations` -d 'id=3'
However, the server is not seeing the parameter id = 3. I tried using some GUI application and when I pass the url as: http://localhost:5000/locations?id=3, it works. I really would rather use CURL rather than this GUI application. Can anyone please point out what I'm doing wrong?
The application/x-www-form-urlencoded Content-type header is not required (well, kinda depends). Unless the request handler expects parameters coming from the form body. Try it out:
curl -X DELETE "http://localhost:5000/locations?id=3"
or
curl -X GET "http://localhost:5000/locations?id=3"
#Felipsmartins is correct.
It is worth mentioning that it is because you cannot really use the -d/--data option if this is not a POST request. But this is still possible if you use the -G option.
Which means you can do this:
curl -X DELETE -G 'http://localhost:5000/locations' -d 'id=3'
Here it is a bit silly but when you are on the command line and you have a lot of parameters, it is a lot tidier.
I am saying this because cURL commands are usually quite long, so it is worth making it on more than one line escaping the line breaks.
curl -X DELETE -G \
'http://localhost:5000/locations' \
-d id=3 \
-d name=Mario \
-d surname=Bros
This is obviously a lot more comfortable if you use zsh. I mean when you need to re-edit the previous command because zsh lets you go line by line. (just saying)

Resources