Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place a multi-line single string JSON in POSTMAN?

Tags:

json

post

postman

Here is what I am using in Python 3:

    payload={"query": """query 
       {
          organization(login: "MY-ORG-ID") {
             samlIdentityProvider {
                externalIdentities(first: 10) {
                   edges {
                      node {
                         user {login}
                         samlIdentity {nameId}
                         scimIdentity {username}
                      }
                   }
                }
             }
          }
       }"""
    }

URL     = 'https://api.github.com/graphql'
HEADERS = {'accept': 'application/vnd.github.v4.idl', 'authorization': 'bearer MY-GITHUB-TOKEN'}
response = requests.post(url=URL, json=payload, headers=HEADERS)

It just works fine.

However, I am trying to use this query in POSTMAN tool but have no clue how to do this. I tried to remove 3-double quotes """ """, I get Unexpected 'q' error. When I use double quotes instead of 3-double quotes and login: \"MY-ORG-ID\", I get "message": "Problems parsing JSON" error.

There's no problem with headers and URL. I just gave them here for completeness.

like image 210
Rafiq Avatar asked Sep 25 '17 23:09

Rafiq


3 Answers

If you're trying to enter the query into body of your post request in the postman app, a quick workaround to achieve multiple lines is to use a placeholder in the form of an environment variable in your body and enter the query in your pre-request script:

In your body:

{
"query":{{query}}
}

In your pre-request script:

pm.environment.set("query", JSON.stringify(
    `
    query {
       organization(login: "MY-ORG-ID") {
          samlIdentityProvider {
             externalIdentities(first: 10) {
                edges {
                   node {
                      user {login}
                      samlIdentity {nameId}
                      scimIdentity {username}
                   }
                }
             }
          }
       }
    }
    `
));

Note that ` in the above code is a backtick, not a single quote!

It's not the best solution ever, but the only one that worked for me so far in Postman to avoid entering more complex queries/mutations in a single line.

Hope this helps.

like image 103
DennisS1981 Avatar answered Oct 27 '22 11:10

DennisS1981


Postman has a "graphql" type of request body. It means you can write your query without quotes (see screenshot attached). Also, it is useful when you are assigning variables to query/mutation.

P.S. you might need to update your postman to get a "graphql" type of body payload.

screenshot from Postman UI with "graphql" radio button

like image 44
Alex Zheka Avatar answered Oct 27 '22 13:10

Alex Zheka


Apparently you can't, therefore you need to turn your multiline string into a single string.

Quickest way to do this is to paste it in a web browser search bar for a format change, then copy and paste from the web browser search bar back into postman.

like image 29
lobi Avatar answered Oct 27 '22 12:10

lobi