Python requests and Graphql API: How to use variables?

Python requests and 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.

2

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

    – jkr

    Oct 3 at 15:30


  • It did not work for me I tried using that.

    – Karthik Bhandary

    Oct 4 at 4:20


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

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