How to send data using curl from Linux command line?
there is a "--post-file" option in wget:
wget --post-file=filetoSend URL
Send request to cURL with post data sourced from a 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.
How to post raw body data with curl?
curl's --data
will by default send Content-Type: application/x-www-form-urlencoded
in the request header. However, when using Postman's raw
body mode, Postman sends Content-Type: text/plain
in the request header.
So to achieve the same thing as Postman, specify -H "Content-Type: text/plain"
for curl:
curl -X POST -H "Content-Type: text/plain" --data "this is raw data" http://78.41.xx.xx:7778/
Note that if you want to watch the full request sent by Postman, you can enable debugging for packed app. Check this link for all instructions. Then you can inspect the app (right-click in Postman) and view all requests sent from Postman in the network
tab :
cURL http post file upload using curl --data in linux command line
You usually can't simply pick -F or -d (--data) at your choice. The web server that will receive your post expects one of the formats. If the form you're trying to submit uses the type 'multipart/form-data', then and only then you must use the -F type. If not, you should use -d which then causes a posting with the type 'application/x-www-form-urlencoded'.
multipart/formposts with -F uses a special formatting of the post with Mime-headers separating the different parts and each part having its own set of headers.
-d is just raw data being sent for the server to interpret/decode.
Mentioned in the curl FAQ.
(But since you write the PHP code in this case, you just have to decide which POST method you want to accept and then make your curl command line use that.)
How do I POST JSON data with cURL?
You need to set your content-type to application/json. But -d
(or --data
) sends the Content-Type application/x-www-form-urlencoded
, which is not accepted on Spring's side.
Looking at the curl man page, I think you can use -H
(or --header
):
-H "Content-Type: application/json"
Full example:
curl --header "Content-Type: application/json" \
--request POST \
--data '{"username":"xyz","password":"xyz"}' \
http://localhost:3000/api/login
(-H
is short for --header
, -d
for --data
)
Note that -request POST
is optional if you use -d
, as the -d
flag implies a POST request.
On Windows, things are slightly different. See the comment thread.
Using curl POST with variables defined in bash script functions
You don't need to pass the quotes enclosing the custom headers to curl. Also, your variables in the middle of the data
argument should be quoted.
First, write a function that generates the post data of your script. This saves you from all sort of headaches concerning shell quoting and makes it easier to read an maintain the script than feeding the post data on curl's invocation line as in your attempt:
generate_post_data()
{
cat <<EOF
{
"account": {
"email": "$email",
"screenName": "$screenName",
"type": "$theType",
"passwordSettings": {
"password": "$password",
"passwordConfirm": "$password"
}
},
"firstName": "$firstName",
"lastName": "$lastName",
"middleName": "$middleName",
"locale": "$locale",
"registrationSiteId": "$registrationSiteId",
"receiveEmail": "$receiveEmail",
"dateOfBirth": "$dob",
"mobileNumber": "$mobileNumber",
"gender": "$gender",
"fuelActivationDate": "$fuelActivationDate",
"postalCode": "$postalCode",
"country": "$country",
"city": "$city",
"state": "$state",
"bio": "$bio",
"jpFirstNameKana": "$jpFirstNameKana",
"jpLastNameKana": "$jpLastNameKana",
"height": "$height",
"weight": "$weight",
"distanceUnit": "MILES",
"weightUnit": "POUNDS",
"heightUnit": "FT/INCHES"
}
EOF
}
It is then easy to use that function in the invocation of curl:
curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data "$(generate_post_data)" "https://xxx:xxxxx@xxxx-www.xxxxx.com/xxxxx/xxxx/xxxx"
This said, here are a few clarifications about shell quoting rules:
The double quotes in the -H
arguments (as in -H "foo bar"
) tell bash to keep what's inside as a single argument (even if it contains spaces).
The single quotes in the --data
argument (as in --data 'foo bar'
) do the same, except they pass all text verbatim (including double quote characters and the dollar sign).
To insert a variable in the middle of a single quoted text, you have to end the single quote, then concatenate with the double quoted variable, and re-open the single quote to continue the text: 'foo bar'"$variable"'more foo'
.
Related Topics
Individual Thread Priority Checking Using Command Line in Linux
Linux Async (Io_Submit) Write V/S Normal (Buffered) Write
Prevent Fork() from Copying Sockets
Error Message When Setting Up Adt in Linux
Delete Line from Text File with Line Numbers from Another File
Sdl Configuration in Eclipse Ide
Qxcbconnection: Could Not Connect to Display Aborted, When Installing Qt on Linux
Linux Patch Ignoring Line Numbers
Naming Convention for Posix Flags
Scale PDF to Add Border for Printing Full Size Pages
Pyinstaller on 32-Bit Linux - Importerror: The 'six' Package Is Required
Can Linux Flock(Fd, Lock_Ex|Lock_Nb) Fail Spuriously
How to Execute The Vim Commands Through Shell Script
How to View Dask Dashboard When Running on a Virtual Machine