How to make a GET request using curl
For more information about curl
, checkout the “What is curl” article. This article will discuss how to interact with an API using GET
requests through curl.
Make a simple GET
request
The basic syntax for sending a GET
request using curl is:
1curl https://example.com
OR
1curl -X GET https://example.com
NOTE: The -X
flag is a shorthand for --request
. It’s not required because the default value is GET
.
Make a GET
request for JSON data
If the server responds differently per the Accept
header, users can specify for it to accept JSON data.
1curl \
2 -H "Accept: application/json" \
3 https://jsonplaceholder.typicode.com/posts/1
In the example above, a GET
request is sent to the JSONPlaceholder API to fetch a post with an id of 1
with an Accept
header of application/json
.
The -H
flag accepts a Key: value
string that represents a header.
NOTE: The -H
flag is a shorthand for --header
. Either can be used.
This same technique may be used for other mime types:
application/xml
text/html
img/png
- etc.