- HTTP POST and GET using cURL in Linux [duplicate]
- 2 Answers 2
- Support
- Popular curl Examples
- What is curl?
- curl examples
- 1. HTTP GET request
- 2. Returning only the HTTP headers of a URL
- 3. Saving the result of a curl command
- 4. Adding an additional HTTP request header
- 5. Generating additional information
- 6. Resuming a download
- 7. Storing HTTP headers
- 8. Testing the download time of an asset without any output
- 9. Specifying a maximum transfer rate
- 10. HTTP/2 support check
- 11. Retrieving a particular byte-range
- 12. curl usage help
- curl examples to simulate HTTP methods
- 13. GET method
- 14. POST method
- 15. DELETE method
- 16. PUT method
- 17. Making curl requests with data
- Summary
- Supercharge your content delivery 🚀
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
Support
Find answers, guides, and tutorials to supercharge your content delivery.
Popular curl Examples
What is curl?
curl, short for «Client for URLs», is a command line tool for transferring data using various protocols. This tool has applications in many household products such as tablets, printers, cars, routers, etc.
There are a vast amount of use-cases for curl, such as:
This tool also supports the use of all the following protocols: DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, and TFTP.
This guide will outline a few popular curl examples, along with a description of what each command does.
curl examples
The following commands can all be entered directly into your terminal to retrieve a response.
1. HTTP GET request
The first example is the most basic example which demonstrates a simple curl command that simulates a GET request for a website URL. This command will output the HTTP response of the URL in question.
2. Returning only the HTTP headers of a URL
The -I option is used to tell curl to only fetch the HTTP headers ( HEAD method) of a particular page or resource.
3. Saving the result of a curl command
The -o and -O curl options are used to save the result of the curl command. The difference between both options is that -o will save the file with a predefined filename, which in this case is test.jpg . On the other hand, the -O option will save the file as its exisiting name, which is example.jpg . An example of each scenario is shown below.
4. Adding an additional HTTP request header
This curl command has the ability to add an additional HTTP request header to your requests. Simply use the -H option and set the header name and value in enclosed quotes. If you do not define a value for the header then the header itself must be followed by a semicolon (e.g. X-Header; ).
5. Generating additional information
The -v option (for verbose) can be defined within a curl command so that it will generate more information during the operation. For example, using the additional header example from above, we can add the -v option which will display more information in regards to the connection and will display the custom header as well as regular headers.
6. Resuming a download
If a download is started for a particular asset but gets interrupted or purposely stopped, it can easily be resumed with the -C option. Simply add -C — to the curl command in question and the asset will resume downloading where it left off.
7. Storing HTTP headers
With the -D option, you have the ability to store the HTTP headers that a site sends back. This is useful for instance if you want to read the cookies from the headers by using a second curl command and including the -b option. The — after the -D tells curl that the output file is stdout (the file into which kernel writes its output).
8. Testing the download time of an asset without any output
The following command uses a couple of curl options to achieve the desired result. The -D — tells curl to store and display the headers in stdout and the -o option tells curl to download the defined resource. However, if you do not want any output, simply add /dev/null to the end of the command. This example can be useful if you are testing the download speed of an asset but don’t want to print or save the output.
9. Specifying a maximum transfer rate
You can specify the maximum transfer rate for both uploads and downloads with the —limit-rate option. The rate is measured in bytes/second unless a suffix, such K for kilobytes, M for megabytes, and G gigabytes, is added to the end of the specified number.
10. HTTP/2 support check
If you have the latest curl release, you can use the —http2 option to check if a particular URL supports the new HTTP/2 protocol. Therefore, if the site does support HTTP/2, you will see HTTP/2.0 200 in the header instead of HTTP/1.1 200 .
11. Retrieving a particular byte-range
Use the -r option to retrieve a particular byte-range of a document. This essentially means to retrieve a particular portion of a file. These byte-range commands can be specified in a number of ways (e.g. 0-499 or 500-999 ). Read our article to learn more about Byte-Range Requests.
12. curl usage help
Run the -h option to quickly retrieve a list of helpful command line options with associated descriptions.
curl examples to simulate HTTP methods
curl can also be useful for testing HTTP methods. The following is a list of request methods that can be used by running a curl command.
13. GET method
The GET method is used to retrieve resources from a particular URL. The simple curl https://www.keycdn.com command will use GET as the default HTTP method, however it can also be specified using —request GET or -X GET .
14. POST method
The POST method is used to post information to a web server (e.g. a comment on a forum). This can be specified using —request POST or -X POST .
15. DELETE method
The DELETE method deletes the resource from the web server associated with a specific URL. This can be specified using —request DELETE or -X DELETE .
16. PUT method
The PUT method creates or replaces a resource based on the data the client submits to the web server. (e.g creating a new web page or updating an existing one). This can be specified using —request PUT or -X PUT .
17. Making curl requests with data
You can make requests using certain HTTP methods and also send along data via the -d or —data option. The example below uses a POST request which sends login data to a login page of a particular website.
Summary
The above curl examples are amongst some of the most used and most popular. curl is a very useful tool for a variety of purposes including debugging, development, etc. Use this curl reference guide to help define specific curl examples of your own and visit the curl man page for a full list of curl options.
Supercharge your content delivery 🚀
Try KeyCDN with a free 14 day trial, no credit card required.