DEV Community

Tikam Singh Alma
Tikam Singh Alma

Posted on

3 1

Graphql requests with CURL

Make a GraphQL query request


 

curl 'https://countries.trevorblades.com/' \

-X POST \

-H 'content-type: application/json' \

--data '{

"query": "{ continents { code name } }"

}'
Enter fullscreen mode Exit fullscreen mode

Make a mutation request


 

curl 'https://graphql-api-url' \

-X POST \

-H 'content-type: application/json' \

--data '{

"query":"mutation { createUser(name: \"John Doe\") }"

}'
Enter fullscreen mode Exit fullscreen mode

Pass additional headers 

If you need to pass authorization in a header, add another -H argument:

curl 'https://countries.trevorblades.com/' \
  -X POST \
  -H 'Authorization: Token xxxxx' \
  -H 'content-type: application/json' \
  --data '{
    "query": "{ continents { code name } }"
  }'
Enter fullscreen mode Exit fullscreen mode

Printing response headers 

If you want to see response headers, add -D - (dump headers to stdout):

curl 'https://countries.trevorblades.com/' \
  -X POST \
  -D - \
  -H 'content-type: application/json' \
  --data '{
    "query": "{ continents { code name } }"
  }'
Enter fullscreen mode Exit fullscreen mode

Pretty printing response JSON 

If you want to pretty print the output JSON, add | python -m json.tool in the end of the curl call.

curl 'https://countries.trevorblades.com/' \
  -X POST \
  -H 'content-type: application/json' \
  --data '{
    "query": "{ continents { code name } }"
  }' | python -m json.tool
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs