lundi 24 août 2020

How to run GraphQL request in Pre-Request Script section in postman?

I want to run a query before the actual request runs and get a value from the pre-request response and set it in a collection variable. I have a problem running the following as I used to do it while testing REST APIs.

This is what I tried to do

const getUserBeforeUpdate = {
  url: pm.environment.get("base-url"),
  method: 'POST',
  header: {
   'content-type': 'application/json',
   'Authorization': `Bearer ${pm.environment.get("token")}`},
  body: JSON.stringify({query: '{ user { profile {id} } }'})
};
 
 pm.sendRequest(getUserBeforeUpdate, function(err, response) {
    pm.expect(response.code).to.eql(200);
   
    // set collection variable from the response
 });

but I get a console error stating

There was an error in evaluating the Pre-request Script:  Error: Unexpected token u in JSON at position 0

What's the right way to chain requests in graphql?

Aucun commentaire:

Enregistrer un commentaire