Gopard
Gopard

Reputation: 1060

Graphql post body "Must provide query string."

I use Express-graphql middleware. I send the following request in the body line:

POST /graphql HTTP/1.1
Host: local:8083
Content-Type: application/graphql
Cache-Control: no-cache
Postman-Token: d71a7ea9-5502-d5fe-2e36-0ae49c635a29

{
   testing {
      pass(id: 1) {
        idn
      }
    }
}

and have error

{
  "errors": [
    {
      "message": "Must provide query string."
    }
  ]
}

in graphql i can send update in URL.

URL string is too short. i must send update model like

mutation {
  update(id: 2, x1: "zazaza", x2: "zazaza", x3: "zazaza" ...(more more fields)...) {
    idn
  }
}

I think its must be in request body. How can I send 'update' query or that I'm doing wrong?

Upvotes: 60

Views: 81519

Answers (9)

Prasanth Jaya
Prasanth Jaya

Reputation: 4736

If you are using graphql and want to test it using postman or any other Rest client do this.

In postman, select POST method and enter your URL and set Content-Type as application/graphql then pass your query in the body.

Example:

http://localhost:8080/graphql
Method: POST
Content-Type: application/graphql
Body: 
  query{
    FindAllGames{
    _id
    title
    company
    price
    year
    url
   }
 }

Thats it you will get the response.

enter image description here

Upvotes: 14

Mad Bernard
Mad Bernard

Reputation: 372

I was seeing this error coming out of a test I was writing, and when I looked into it further, I found that I was the only one in the codebase who had ever tried using the homemade test harness's method for changing variables... and that method did not work.

The test literally wasn't sending a query string. Just mentioning in case others encounter a similar issue.

Upvotes: 0

Gopard
Gopard

Reputation: 1060

Post request needs to manage headers info.

  1. Using Http client - Content-Type: application/json

  2. Using Postman client - Content-Type: application/graphql

but request body looks like string

{"query":"mutation{update(id:1,x1:\"zazaz\",x2:\"zazaz\"......){id x1 x2}}"}

Upvotes: 71

wowkin2
wowkin2

Reputation: 6355

Check if you are using correct protocol in your Postman requests.

I used HTTP instead of HTTPS and this caused the same error.

Changes of content-type, raw or json instead of graphql type didn't help.

Upvotes: 1

Shivam Pandey
Shivam Pandey

Reputation: 94

This generally occurs when your 'express-graphql' doest receive any params. You need to added a json/applicaton parser in your application.

npm install body-parser

eg -

const bodyParser = require('body-parser');

app.use(bodyParser.json()); // application/json

Upvotes: 6

gbengaoyetade
gbengaoyetade

Reputation: 864

Using Postman Version 7.2.2 I had a similar issue. This version of Postman supports Graphql out of the box. Changing the Content-type to application/json fixed it for me.

Image showing the Content-type in Postman

Upvotes: 7

user10334760
user10334760

Reputation:

Switch content type to JSON. Like this see Postman image

Upvotes: 1

Crystyan S. Santos
Crystyan S. Santos

Reputation: 61

for me worked like as following:

In the body

Body postman

In the Headers

Header postman

Don't forget mark GraphQl [x] on Body settings

enter image description here

And how was quoted before changes the verb to POST.

Upvotes: 5

janadari ekanayaka
janadari ekanayaka

Reputation: 5116

  1. go to the relevant web page and open "inspect" (by write click -> inspect || Ctrl+Shift+I in chrome)

  2. go to the network tab and copy the cURL command enter image description here

  3. open the postman ,then import -> raw text

  4. paste the copied command enter image description here

  5. then,continue ->

enter image description here

Upvotes: 4

Related Questions