Making HTTP requests to a GraphQL API

Query a GraphQL API using HTTP requests.

This section provides a deeper look into how GraphQL APIs are accessed over HTTP. Whether you're using tools like curl, Postman, or writing code, understanding the structure of a GraphQL HTTP request is essential for effective API interaction.

Overview

Unlike REST APIs that expose multiple endpoints for different resources, GraphQL APIs use a single endpoint to handle all queries and mutations. This makes the request structure more consistent but also requires a clear understanding of how to format your HTTP request

HTTP request structure
A typical GraphQL HTTP request includes:
Table 1. HTTP request
Component Description
Method Post
Endpoint A single URL (e.g., https://api.example.com/graphql)
Headers Usually includes Content-Type, Accept, and Authorization
Body JSON object with query, and optionally variables and operationName
Example request body
{
  "query": "query { allFilms { films { title director releaseDate } } }"
}
Using curl
Here’s how to send a GraphQL query using curl:
curl 'https://your-api-endpoint/graphql' \
  -X POST \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json' \
  -H 'Authorization: Apikey YOUR_API_KEY' \
  --data-binary '{"query":"query { allFilms { films { title director releaseDate } } }"}'
Table 2. Curl request
Parameters Description
-X POST: Specifies the HTTP method.
-H 'Content-Type: application/json': Tells the server you're sending JSON.
-H 'Authorization: Apikey YOUR_API_KEY': Authenticates your request.
--data-binary: Sends the GraphQL query as the request body.