Simple HTTP POST request is failing

Hello,

I’ve been playing with the Dgraph Cloud for a few days, and I’ve been trying to make a simple HTTP POST request to the backend, but it’s failing.

This is my code:

fetch(
  `https://truncated.ap-south-1.aws.cloud.dgraph.io/admin`,
  {
    method: 'POST',
    headers: {
      'Dg-Auth': DGRAPH_API_KEY,
      'Content-Type': 'application/json',
    },
    body: '{"query":"{ getGQLSchema { schema } }"}',
  }
)
  .then((response) => {
    return response.json()
  })
  .then((data) => {
    console.log(data)
  })
  .catch((error) => {
    console.log(error)
  })

These are the response headers:

HTTP/2 401 Unauthorized
access-control-allow-credentials: true
access-control-allow-headers: Content-Type, Accept-Encoding, Content-Length, X-Auth-Token, sentry-trace, DG-Auth, sentry-trace, DG-Auth
access-control-allow-methods: *
access-control-allow-origin: *
content-type: application/json
server: Caddy
content-length: 52
date: Sun, 18 Apr 2021 12:30:51 GMT
X-Firefox-Spdy: h2

These are the request headers:

POST /query HTTP/2
Host: [rough-firefly.ap-south-1.aws.cloud.dgraph.io](http://rough-firefly.ap-south-1.aws.cloud.dgraph.io)
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: http://localhost:8000/en/
Dg-Auth: <...>
Content-Type: application/json
Origin: http://localhost:8000
Content-Length: 39
Connection: keep-alive
DNT: 1
Sec-GPC: 1
TE: Trailers

This is based on your documentation here – https://dgraph.io/docs/cloud/admin/authentication/

Can you please review?

PS: I’ve set the relevant CORS header in my admin dashboard.

Try JSON.stringify() in the body like so:

fetch('https://truncated.ap-south-1.aws.cloud.dgraph.io/admin', {
  method: 'POST',
  headers: {
    'Dg-Auth': DGRAPH_API_KEY,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    query: `your-query`,
    variables: `{ if there are variables here... }`
  })
});