POST method using Python requests

We can also post data to a GraphQl API using the requests library. We simply have to follow the schema and parse the data correctly. We are using the Fruit API again to post the data to it. So, we have a different type of schema, instead, we have a mutation to write instead of a query. We follow up the documentation to the right to get the schema.

We can now write up the schema and parse the attributes which we want to return after the mutation has been saved i.e. the POST request has been made, we will parse the required data.

 

Here, we are creating a mutation with the attribute name and the value which we want to assign so as the key-value map. After the mutation, we query the attributes which we want, here we are querying the attributes like id, scientific_name, tree_name, fruit_name, and origin. But these can be any other attributes or all attributes depending on the requirement. 

Python3




# POST Request 
import requests
  
url = "https://fruits-api.netlify.app/graphql"
  
body = """
mutation {
  addFruit(
    id: 1
    scientific_name: "mangifera"
    tree_name: "mangifera indica"
    fruit_name: "Mango"
    family: "Anacardiaceae"
    origin: "India"
    description: "Mango is yellow"
    bloom: "Summer"
    maturation_fruit: "Mango"
    life_cycle: "100"
    climatic_zone: "humid"
  ) {
    id
    scientific_name
    tree_name
    fruit_name
    origin
  }
}
"""
  
response = requests.post(url=url, json={"query": body})
print("response status code: ", response.status_code)
if response.status_code == 200:
    print("response : ",response.content)


Output:

 

The POST method is used in the above script for posting the data so the requests.post method as previously said takes in a couple of arguments like URL and JSON, it also takes more arguments but is optional and dependent on the request we are making. 

GET and POST Requests in GraphQL API using Python requests

In this article, we will be understanding how to write GET and POST requests to GRAPHQL APIs using the Python request module. Dealing with GraphQL API is a bit different compared to the simple REST APIs. We have to parse in a query that involves parsing the GraphQL queries in the request body.

Similar Reads

What are Requests in Python

Requests is a python package/library which helps in working with HTTP requests using Python. We can very conveniently parse the body, headers, and other parameters to a request using methods provided by the requests library like, GET, PUT, POST, DELETE, etc. Using the requests module we will be parsing the queries to a GraphQL API....

What is GraphQL API In Python

GraphQL API is a query language for fetching data from a database. It has a different approach than traditional REST APIs. Using GraphQL API, we can fetch the only required information, this avoids the drawbacks of under fetching and over fetching compared to the REST APIs. This type of API allows us to fetch very fine-tuned data in a defined schema so that the application can consume it, only the data which is required will be fetched using the GraphQl queries to the API....

GET method using Python requests

We can send a GET request to the given GraphQL API with the get method provided in the requests library which we imported.  Here due to the design of the API we have to use the POST method to fetch the results but it would depend on the API, we will dive into that later....

POST method using Python requests

...

UPDATE method using Python requests

...

DELETE method using Python requests

We can also post data to a GraphQl API using the requests library. We simply have to follow the schema and parse the data correctly. We are using the Fruit API again to post the data to it. So, we have a different type of schema, instead, we have a mutation to write instead of a query. We follow up the documentation to the right to get the schema....