Graphql & requests package py: How to use variables in the query body

Graphql & requests package py: How to use variables in the query body


-2

I am using requests package from python to use graphql api to create users in my wiki.js I have the following code where I want to use variables for email and name fields. They work when hard coded but I need it to work with variables. Code:

# 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 #want to use a variable 
name: name #want to use a variable
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)


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

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