Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST URL in data of a curl request

I am trying to post two parameters using curl, path and fileName:

curl --request POST 'http://localhost/Service' --data "path='/xyz/pqr/test/'&fileName='1.doc'" 

I know something is wrong in this. I have to use something like URLEncode. I tried many things still no luck.

Please give an example how can I post the url in data of curl request.

like image 802
Vivek Muthal Avatar asked Aug 13 '13 08:08

Vivek Muthal


People also ask

How do I hit a URL in curl?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option.

How do I send a POST request URL?

POST request in itself means sending information in the body. I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters. Just use your URL in the place of theirs.

How do you post form data using curl?

To post form data with Curl, you can use one of two command-line parameters: -F (--form) or -d (--data). The -F command-line parameter sends form data with the multipart/form-data content type, and the -d command-line parameter sends form data with the application/x-www-form-urlencoded content type.


2 Answers

Perhaps you don't have to include the single quotes:

curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/&fileName=1.doc" 

Update: Reading curl's manual, you could actually separate both fields with two --data:

curl --request POST 'http://localhost/Service' --data "path=/xyz/pqr/test/" --data "fileName=1.doc" 

You could also try --data-binary:

curl --request POST 'http://localhost/Service' --data-binary "path=/xyz/pqr/test/" --data-binary "fileName=1.doc" 

And --data-urlencode:

curl --request POST 'http://localhost/Service' --data-urlencode "path=/xyz/pqr/test/" --data-urlencode "fileName=1.doc" 
like image 128
konsolebox Avatar answered Oct 13 '22 20:10

konsolebox


I don't think it's necessary to use semi-quotes around the variables, try:

curl -XPOST 'http://localhost/Service' -d "path=%2fxyz%2fpqr%2ftest%2f&fileName=1.doc" 

%2f is the escape code for a /.

http://www.december.com/html/spec/esccodes.html

Also, do you need to specify a port? ( just checking :) )

like image 37
Patrick Avatar answered Oct 13 '22 20:10

Patrick