Flutter and Dgraph

Hi,

I am starting dgraph using docker-compose. Zero, Server and Ratel-ui. Which is great and it works.

I created a flutter app from which i am making a Http Request. However when i do so, the server return an error saying the below:

Request parsed in google chrome

POST /query HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 195
Accept: */*
DNT: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
Content-Type: application/json; charset=utf-8
Origin: http://127.0.0.1:43939
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://127.0.0.1:43939/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

Request Payload

{"query":"{\n  get_orders(func: eq(name, \"Test Business\"))\n  {\n    uid\n    name\n    hasOrder {\n      description\n      hasProducts {\n        name\n      }\n    }\n  }\n}","variables":{}}

Error Response

{"errors":[{"message":"Unsupported Content-Type. Supported content types are application/json, application/graphql+-","extensions":{"code":"ErrorInvalidRequest"}}]}

I verified the same request is being made when i use the UI and unable to why this wouldnt work. Any help would be greatly appreciated.

change this to:
application/json

Hey @harshadbhatia,

You are facing this issue because of the “Content-Type”, as you said that it is working for ratel, it is because that the content type in that request is “application/json”, which is also evident from your request in Chrome.

But when you are making this request from the flutter app, I guess that the default request is treated as a raw request in which the content type is 'Content-Type: text/plain'. Make sure you specify the content type as application/json there.

Yes but if you see the request headers from chrome as above is application json. The request above is what is sent by code below.

My flutter code doing same:

http.post(Uri.encodeFull("http://localhost:8080/query"),
          headers: {
            'Content-Type': "application/json"
          },
          body: json.encode({"query": query, "variables": {}}),

Is there a way i can debug request sent to docker image ?

Yes, you can use log level 3. But I don’t think it will help, as the issue for sure is in the Flutter app.

dgraph alpha -h | grep "log level"
  -v, --v Level                          log level for V logs

You have to make sure it is

application/json

Also, install Insomnia and test out there instead of Chrome.

BTW, have you tested this lib?

1 Like

Moving to Users category, considering this is a question.