Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 228
Randhi Rupesh Avatar asked Feb 20 '13 11:02

Randhi Rupesh


People also ask

Does curl do get or POST?

"By default you use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like curl example.com it will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT." Everything you need to know.

How do I curl POST request?

Users can send data with the POST request using the -d flag. The following POST request sends a user and a pass field along with their corresponding values. POSTing with curl's -d option will include a default header that looks like: Content-Type: application/x-www-form-urlencoded .

What is a curl HTTP POST?

HTTP POST - Everything curl. HTTP POST. POST is the HTTP method that was invented to send data to a receiving web application, and it is how most common HTML forms on the web works. It usually sends a chunk of relatively small amounts of data to the receiver.

How do I request curl in Linux?

The syntax for the curl command is as follows: curl [options] [URL...] In its simplest form, when invoked without any option, curl displays the specified resource to the standard output. The command will print the source code of the example.com homepage in your terminal window.


2 Answers

*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 
like image 57
Amith Koujalgi Avatar answered Oct 26 '22 09:10

Amith Koujalgi


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

like image 38
grepit Avatar answered Oct 26 '22 11:10

grepit