DEV Community

Tikam Singh Alma
Tikam Singh Alma

Posted on

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

Top comments (0)