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:
|
|
OR
|
|
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.
|
|
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.