419

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?

1
  • 3
    For building a curl command quickly I'm using this online tool: curlbuilder.com
    – Micer
    May 17, 2020 at 15:12

2 Answers 2

810

*nix provides a nice little command which makes our lives a lot easier.

GET:

with JSON:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

with XML:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource

POST:

For posting data:

curl --data "param1=value1&param2=value2" http://hostname/resource

For file upload:

curl --form "[email protected]" http://hostname/resource

RESTful HTTP Post:

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:

For JSON:

If you use npm and nodejs, you can install json package by running this command:

npm install -g json

Usage:

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:

pip install pjson

Usage:

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.

Usage:

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

Usage:

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

Usage:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource |  json_reformat

For XML:

If you use *nix with Debian/Gnome envrionment, install libxml2-utils:

sudo apt-get install libxml2-utils

Usage:

curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource | xmllint --format -

or install tidy:

sudo apt-get install tidy

Usage:

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

or

curl http://hostname/resource -o /path/to/your/file

For detailed description of the curl command, hit:

man curl

For details about options/switches of the curl command, hit:

curl -h
8
  • 1
    If you're redirecting to less, you might need to add -sS.
    – x-yuri
    Dec 25, 2014 at 2:47
  • How can i get the response to put into another command line? Sep 1, 2015 at 15:18
  • @emoleumassi see x-yuri's comment before yours; you should be able to pipe the return into another command, such as less. Jul 21, 2016 at 14:25
  • 1
    Potentially better than the grep approach below is to pipe the results to jq.
    – grant
    Jan 25, 2017 at 22:43
  • 6
    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"
    – Ricardo
    Jun 26, 2017 at 22:44
58

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 '{"id":"IDVALUE","name":"Mike"}' | grep }| python -mjson.tool

enter image description here

1
  • 3
    What is grep supposed to be doing here? I use the following with the same result: curl -s -H "Accept: application/json" -H "Content-Type: application/json" -H "http://${API_BASE_URL}${1}" | python -mjson.tool Jul 21, 2016 at 14:14

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