What is Curl?Curl is a command-line tool that allows developers and administrators to transfer data over the network. Curl supports over 25 protocols, including HTTP, HTTPS, FTP, and SFTP. When it comes to debugging network requests and API calls, as well as automating your day-to-day routines, Curl is one of the best tools you can find on the market. It is versatile, powerful, and works on Linux, Mac, and Windows.
What is JSON?
How to make GET request with Curl?
The GET request method is the default HTTP method when requesting a resource from the server. Curl always sends a GET request unless you explicitly tell Curl to use a different HTTP method with the -X command-line option, or you send data with the --data or --form command-line options. You can also use the --head command-line option to tell Curl to use the HTTP HEAD method instead of the HTTP GET method.
Curl GET request syntax
The general form of a Curl command for making a GET request is as follows:
- [options] - some options for the Curl command. For example, you can send an Accept HTTP header to the server with the GET request with -H "Accept: application/json" command-line option.
Why is it important to specify the correct Accept header when getting JSON with Curl?
The server may serve data in different formats from a single API endpoint. In this case, the server looks at the Accept request header and selects the best MIME type for the client. If you do not send the "Accept: application/json" header with your request, the server may choose to send data of a different type other than JSON. To explicitly tell the server to send data in JSON format, we need to add the "Accept: application/json" header to our request with the Curl -H "Accept: application/json" command-line option.
Can I send JSON in the body of an HTTP GET request?
No, you cannot send JSON and any other data in the body of an HTTP GET request. Sending the body/payload in a GET request message may cause the server to reject the request. If you need to pass data to the server, use URL parameters for this.
Curl GET Request Examples
Below are Curl examples to get JSON, XML and HTML data from the server.
How to get JSON data with Curl?
The following is an example of getting JSON data using Curl from the server:
How to get XML data using Curl?
The following is an example of getting XML data using Curl from the server:
How to get the content of an HTML page using Curl?
The following is an example of getting the content of an HTML page using Curl from the server: