HTTP POST and GET using cURL in Linux [duplicate]
I have a server application written in ASP.NET on Windows that provides a web service. How can I call the web service in Linux with cURL?
2 Answers 2
*nix provides a nice little command which makes our lives a lot easier.
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
curl --data "param1=value1¶m2=value2" http://hostname/resource
curl --form "fileupload=@filename.txt" http://hostname/resource
curl -X POST -d @filename http://hostname/resource
For logging into a site (auth):
curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login curl -L -b headers http://localhost/
Pretty-printing the curl results:
If you use npm and nodejs , you can install json package by running this command:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json
If you use pip and python , you can install pjson package by running this command:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | pjson
If you use Python 2.6+, json tool is bundled within.
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | python -m json.tool
If you use gem and ruby , you can install colorful_json package by running this command:
gem install colorful_json
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | cjson
If you use apt-get (aptitude package manager of your Linux distro), you can install yajl-tools package by running this command:
sudo apt-get install yajl-tools
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource | json_reformat
If you use *nix with Debian/Gnome envrionment, install libxml2-utils :
sudo apt-get install libxml2-utils
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | tidy -xml -i -
Saving the curl response to a file
curl http://hostname/resource >> /path/to/your/file
curl http://hostname/resource -o /path/to/your/file
For detailed description of the curl command, hit:
For details about options/switches of the curl command, hit:
@emoleumassi see x-yuri’s comment before yours; you should be able to pipe the return into another command, such as less .
On the get example, you may quote the whole url to avoid errors on params, e.g. curl «http://www.virustotal.com/vtapi/v2/ip-address/report?ip=8.8.8.8&apikey=1233456890»
I think Amith Koujalgi is correct but also, in cases where the webservice responses are in JSON then it might be more useful to see the results in a clean JSON format instead of a very long string. Just add | grep >| python -mjson.tool to the end of curl commands here is two examples:
GET approach with JSON result
curl -i -H "Accept: application/json" http://someHostName/someEndpoint | grep >| python -mjson.tool
POST approach with JSON result
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" http://someHostName/someEndpoint -d '' | grep >| python -mjson.tool
Make HTTP Get Request with curl Command
The curl command is used to make requests and act as a client for a lot of popular network-based protocols. HTTP requests can be created with the curl command easily. HTTP Get request is a popular request to get remote resources via the HTTP protocol. In this tutorial, we will learn how to make HTTP Get requests with the curl command.
Make HTTP Get Request
By default, the curl command makes HTTP requests to the specified URL. So there is no need to explicitly specify the HTTP Get in the curl command. In the following example, we will make an HTTP Get request to the “http://linuxtect.com/“.
Verbose HTTP Get Request
An HTTP Get request made with multiple steps. These steps can be printed to the standard output by using the verbose mode.
Save HTTP Get Response
The HTTP Get response can be printed to the standard output by default. Also, this output can be stored in a file by using the -o options.
curl -o linuxtect.html http://linuxtect.com/
Add Request Header to HTTP Get Request
HTTP headers are used to provide extra parameters for the HTTP Get request. The –header option can be used to add a header for the HTTP Get Request.
curl --header "Content-Type: application/json" http://linuxtect.com/