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:

1
curl https://example.com

OR

1
curl -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.

1
2
3
curl \
	-H "Accept: application/json" \
	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.