简体   繁体   中英

How to place a multi-line single string JSON in 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.

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.

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.

Triple quotes in Python denote a multi-line string right? So try double quotes, and login: \\"MY-ORG-ID\\" and placing the entire query in a single line?

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

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.

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

Postman UI 的屏幕截图,带有“graphql”单选按钮

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM