HTTP POST and GET using cURL in Linux
Categories:
Mastering HTTP POST and GET Requests with cURL in Linux
Learn how to effectively send HTTP POST and GET requests using the cURL command-line tool in Linux. This article covers basic syntax, common options, and practical examples for interacting with web services.
cURL is a powerful command-line tool and library for transferring data with URLs. It supports a variety of protocols, including HTTP, HTTPS, FTP, and more. For web developers and system administrators, cURL is indispensable for testing APIs, downloading files, and interacting with web servers directly from the terminal. This article will focus on its application for HTTP POST and GET requests, which are fundamental operations in web communication.
Understanding HTTP GET Requests with cURL
HTTP GET is used to request data from a specified resource. It should only be used to retrieve data and should not have any other effect on the server (i.e., it should be idempotent). When you type a URL into your browser, you're essentially making a GET request. With cURL, making a GET request is straightforward, as it's the default request method.
curl https://api.example.com/data
A simple cURL GET request to retrieve data from an API endpoint.
curl -H "Accept: application/json" -H "Authorization: Bearer YOUR_TOKEN" https://api.example.com/secured_data
Adding custom HTTP headers like Accept type and Authorization for authenticated GET requests.
curl "https://api.example.com/search?query=linux&limit=10"
Including query parameters in a GET request URL. Note the use of quotes to handle special characters.
HTTP GET Request Flow
Understanding HTTP POST Requests with cURL
HTTP POST is used to send data to a server to create or update a resource. Unlike GET, POST requests can have side effects on the server, such as creating a new entry in a database. When submitting a web form, you are typically performing a POST request. cURL provides several ways to send data with a POST request, including form data and raw JSON payloads.
curl -X POST -d "username=john_doe&password=secret" https://api.example.com/login
Sending URL-encoded form data using the -d
or --data
option. -X POST
explicitly sets the method.
curl -X POST -H "Content-Type: application/json" -d '{"name": "New Item", "value": 123}' https://api.example.com/items
Sending a JSON payload in the request body. Note the Content-Type
header and single quotes for the JSON string.
echo '{"key": "value from file"}' > data.json
curl -X POST -H "Content-Type: application/json" -d @data.json https://api.example.com/upload
Reading POST data from a file using @filename
syntax. This is useful for larger payloads.
HTTP POST Request Flow
Common cURL Options for HTTP Requests
cURL offers a vast array of options to fine-tune your HTTP requests. Here are some commonly used ones that can enhance your debugging and interaction capabilities.
-i
or--include
: Includes the HTTP response headers in the output. Useful for debugging.-v
or--verbose
: Shows more detailed information about what cURL is doing, including request and response headers.-o <file>
or--output <file>
: Writes the output to a specified file instead of stdout.-s
or--silent
: Suppresses cURL's progress meter and error messages, making it useful for scripting.-L
or--location
: Follows HTTP 3xx redirects.-k
or--insecure
: Allows cURL to proceed with "insecure" SSL connections and transfers.
curl -v -i https://httpbin.org/get
Example combining verbose output and including response headers for a GET request.
httpbin.org
provide excellent endpoints to inspect your cURL requests and their corresponding responses.Mastering cURL for HTTP POST and GET requests is an essential skill for anyone working with web services in a Linux environment. Its flexibility and power allow for efficient testing, automation, and direct interaction with APIs, making it an invaluable tool in your technical toolkit.