Python requests & Graphql API: How to use variables?

Python requests & Graphql API: How to use variables?


0

I am working on creating users on my wiki.js using graphql api and python’s request package. I am able to create a user normally(hard coded) but I want to use variables. Here is the code I tried:

# we have imported the requests module 
import requests 
# defined a URL variable that we will be 
# using to send GET or POST requests to the API 
url = "https://wiki.something.com/graphql/" 
email = "[email protected]" 
name = "Uchiha Sasuke" 
body = """
 mutation { 
   users { 
     create ( 
        email: "{email}"
        name: "{name}"
        passwordRaw: "pass123$"
        providerKey: "local" 
        groups: [1] 
        mustChangePassword: true
        sendWelcomeEmail: false 
     ) { 
        responseResult { 
                succeeded 
                slug 
                message 
        } 
        user { 
          id 
        }  
       } 
      } 
     } 
""" 
response = requests.post(url=url, json={"query": body}, headers={"Authorization": "Bearer ..."})
print("response status code: ", response.status_code) 
if response.status_code == 200:
 print("response : ", response.content)

Above code is not working. How should I deal with this? I have taken a look at other questions regarding api and I tried them but it was of no use.

1

  • perhaps you're looking for f-strings. try using body = f""" ...........

    – jkr

    3 hours ago



Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *