HTTP POST and GET using cURL in Linux

In this tutorial, I will show you how to use cURL in Linux for HTTP GET and POST requests in Node.js.

cURL is a powerful command-line tool for making HTTP requests. It supports various protocols, including HTTP and HTTPS, making it an essential tool for developers working with web services. In this tutorial, we will explore how to use cURL in Linux for performing HTTP GET and POST requests, with a focus on Node.js applications.

cURL Basics

This section introduces cURL as a powerful command-line tool for making HTTP requests, highlighting its support for various protocols, including HTTP and HTTPS. The focus is on its importance for developers working with web services.

Making GET Requests

This part demonstrates how to use cURL to make HTTP GET requests. It includes examples for handling JSON and XML responses. The curl command is used to initiate the GET request, and additional tools like json and xmllint are employed for formatting the responses.

JSON Response:

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

XML Response:

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

Making POST Requests

This section focuses on making HTTP POST requests using cURL. It provides examples for posting data, uploading files, and performing RESTful HTTP POST. Authentication is also covered with an example of logging into a site.

Posting Data:

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

File Upload:

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

RESTful HTTP Post:

curl -X POST -d @filename http://hostname/resource

Logging into a Site (Authentication):

curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
curl -L -b headers http://localhost/

Pretty-Printing cURL Results

This part provides techniques for formatting and making the output of cURL commands more readable, especially for JSON and XML responses. Different tools and approaches, such as npm and Node.js, pip and Python, Python 2.6+, gem and Ruby, and apt-get for Debian/Gnome, are mentioned for pretty-printing.

For JSON:

  • Using npm and nodejs:
curl -i -H "Accept: application/json" -X GET http://hostname/resource | json
  • Using pip and Python:
curl -i -H "Accept: application/json" -X GET http://hostname/resource | pjson
  • Using Python 2.6+:
curl -i -H "Accept: application/json" -X GET http://hostname/resource | python -m json.tool
  • Using gem and Ruby:
curl -i -H "Accept: application/json" -X GET http://hostname/resource | cjson
  • Using apt-get (for Debian/Gnome):
curl -i -H "Accept: application/json" -X GET http://hostname/resource | json_reformat

For XML:

  • Using libxml2-utils:
curl -H "Accept: application/xml" -X GET http://hostname/resource | xmllint --format -
  • Using tidy:
curl -H "Accept: application/xml" -X GET http://hostname/resource | tidy -xml -i -

Saving cURL Response to a File

This section explains how to save the output of cURL commands to a file using the >> or -o options. This can be useful for saving responses for further analysis or processing.

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

Advanced Formatting for JSON Responses

This part focuses on advanced formatting for JSON responses. It provides examples using grep and python -mjson.tool for both GET and POST requests to achieve cleaner JSON output.

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

Additional Resources

For a detailed description of the cURL command, you can refer to the manual:

man curl

For details about options/switches of the cURL command:

curl -h

Now, you have a comprehensive guide on using cURL in Linux for making HTTP GET and POST requests in Node.js, along with tips for formatting and saving responses. Feel free to explore more cURL options based on your specific requirements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top