Sending POST Requests with curl
This is a quick post about sending POST requests via the command line. I have been using curl for a while now - and I find it to be a good alternative to wget. I have just found out how to use it to send a POST request from the command line, and I thought that I would share my findings here.
Sending a POST request is really quite simple:
curl -X POST --data-binary "@$input_filename" -o $output_filename $url
$input_filenameis the name of the filename that contains the data that you want to send.
@-can be used to specify stdin, allowing you to pipe the output of the previous command into curl.
- You can also drop at
@symbol and hard-wire the data you want to send into the command itself.
$output_filenameis the name of the file you want to save the response to. You can drop
-o $output_filenameif you want curl to output he result to he standard output for further processing.
If you find that you get some king of error message from the above, you may need to add
--header "Expect: 100-continue" just before the
$url - some servers require this.
--data-binary is rather important, as if you use
--data on it's own, line breaks are not preserved for some bizarre reason.
I am finding that curl is much more powerful that I first expected, as it understands just about any protocol you care to name....I need to experiment with it further.
Curl also has a modular structure to it's command line arguments, so you can tack and extra setting on the end and it will work exactly as you would expect it to (most of the time!).