Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I found a lot of examples on how to use simple POST commands in cURL, but I didn't find examples on how to send full HTTP POST commands, which contain:

  • Headers (Basic Authentication)
  • HTTP Params (s=1&r=33)
  • Body Data, some XML string

All I found is:

echo "this is body" | curl -d "ss=ss&qq=11" http://localhost/

That doesn't work, and it sends the HTTP parameters as the body.

share|improve this question
add comment

2 Answers

HTTP "parameters" are part of the URL:

"http://localhost/?name=value&othername=othervalue"

Basic authentication has a separate option, there is no need to create a custom header:

-u "user:password"

The POST "body" can be sent via either --data (for application/x-www-form-urlencoded) or --form (for multipart/form-data):

-F "foo=bar"                  # 'foo' value is 'bar'
-F "foo=<foovalue.txt"        # the specified file is sent as plain text input
-F "foo=@foovalue.txt"        # the specified file is sent as an attachment

-d "foo=bar"
-d "foo=<foovalue.txt"
-d "foo=@foovalue.txt"
-d "@entirebody.txt"          # the specified file is used as the POST body

--data-binary "@binarybody.jpg"

So, to summarize:

curl -d "this is body" -u "user:pass" "http://localhost/?ss=ss&qq=11"
share|improve this answer
    
Is it possible to do this with PHP's curl? –  SDP Jul 25 '13 at 18:05
    
@Emerson: It should be; PHP's module seems to have all functions that the original C libcurl has, and the above is a fairly basic feature. I don't know the exact function to use, however. If you cannot find it, ask on Stack Overflow. –  grawity Aug 6 '13 at 11:38
add comment

To tell cURL to put the parameters in the URL, you need to tell it to use HTTP GET (vs the default HTTP POST). From the manual:

-G, --get

When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a '?' separator.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.