This article was originally published on my old blog.
Introduction
If you never heard or used the curl command line tool then this post is for you. This post will teach you some basic curl commands. I recommend that you read the official site for more details: http://curl.haxx.se/
Curl Setup
- Download curl zip
- Extract the contents (if you have downloaded the correct version you should find curl.exe)
- Place curl.exe in a folder where you keep your software (e.g. D:\software\curl\curl.exe)
To run curl from the command line
a) Right-hand click on “My Computer” icon
b) Select Properties
c) Click ‘Advanced system settings’ link
d) Go to tab [Advanced] — ‘Environment Variables’ button
e) Under System variable select ‘Path’ and Edit button
f) Add a semicolon followed by the path to where you placed your curl.exe (e.g. ;D:\software\curl)
Now you can run from the command line by typing the following commands.
Get Commands
Basic Get
curl www.google.com
Basic Get output to file
curl -o google.html www.google.com
Specify Header
curl www.google.com -H X-ApiKey:17263jhbjh1gjhgb
Display all response headers and body
curl www.google.com -i
Display only all headers
curl www.google.com -I
Post Commands
Send post request
curl -X POST www.google.com
Send post request with data
curl -X POST www.google.com -d '{ data: value}'
Send JSON data
curl -X POST https://api.cloudmine.me/v1/app/928a78ffd73e4ff78383d1d4c06dd5a7/text \
-d '{"myrandomkey":{"name":"ethan"}}' \
-H X-CloudMine-ApiKey:e90ef1aeaadd48de93b45038ed592a06 \
-H "content-type:application/json"Common content-type: application/xml application/x-www-form-urlencoded
Send delete request
curl -X DELETE www.google.com
Send content in file in request (Great for large amount of data)
curl -X POST www.google.com -d @./path/to/file
HTTP Basic Authentication request
curl -X POST www.google.com -u username:password
Make request verbose
curl www.google.com -v
These commands just provide a small taste of what is possible with curl.
Summary
I hope these commands help you get started in using curl. I use curl to quickly test APIs. It is a very helpful little tool.
Again I would recommend reading the official documentation to understand some of the more advanced features.