How to call the API of AppSync in curl from my local machine?

Viewed 570

I created an API with AppSync. Now I want to call it with curl, and I get the following error: You are not authorized to make this call.

I guessed the following:

curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer da2-XXXXXXXXXXXXXXXXXXXXXXXXXX" -d '{"query":"listMyModelTypes{listMyModelTypes {items {id title}}}"}' https://wuw4mcnvautpl4v5ox33fdzoq.appsync-api.us-east-1.amazonaws.com/graphql

Or should I also include the API ID somewhere in the query?

1 Answers

Making an Appsync query via CURL or Postman depends on getting the request body and headers right. The required headers depend on auth type.

# common variables
API_URL='https://<APPSYNC-ID>.appsync-api.eu-west-1.amazonaws.com/graphql'
QUERY='query GetImages($t: String!) { images(topic:$t) { edges { cursor } } }'
VARIABLES='{"t":"cats"}' # no spaces!

API Key Auth: x-api-key header

API_KEY='da2-XXXXXXXXXXXXXXXXXXXXXXXXXX'

curl -s -XPOST -H "Content-Type:application/graphql" -H "x-api-key:$API_KEY" -d '{"query": "'"$QUERY"'", "variables": '$VARIABLES'}' $API_URL

Token-based Auth (e.g. Cognito): Authorization and host headers

TOKEN='<YOUR JWT AUTH TOKEN HERE>'
HOST='<APPSYNC-ID>.appsync-api.eu-west-1.amazonaws.com'

curl -s -XPOST -H "Content-Type:application/graphql" -H "Authorization:$TOKEN" -H "host:$HOST" -d '{"query": "'"$QUERY"'", "variables": '$VARIABLES'}' $API_URL
Related