Parse json and Post Request using Playwright Python

Tutorial 17 – Parse json and Post API Request using Playwright Python​​ 

What you will Learn in this blog:

  • Read/parse json data

  • Post request

  • POST Request (Register successful)

  • Code​​ snippets

Read/parse json data

Go to​​ https://reqres.in/api/users/​​ 

Notice that 6 users are listed on this page

To parse the json, we can simply use​​ json​​ method

Execute the command, notice that we get the output data in json format, showing the same 6 user ids​​ 

Let us comment line#7.

Let us say we want to fetch the json output of a specifc user id, we can do that by asking user a question and than fetching the details​​ 

When we execute, notice below that we​​ get the details of respective user id

Post request

Launch​​ https://reqres.in/​​ 

Notice below that we need to pass some body as part of POST request (unlike a GET request)​​ 

Let us now see how to construct a ‘Post’​​ request using Playwright python.​​ 

During runtime, we will be asking user the new ‘name’ he wants to post into the server (line#15) and will then catch this name in ‘user’ variable (line#16)

Next, in line#19, we are creating a standard ‘headers’ tag and this would mean that “we would be posting a json request”​​ 

In line#20 (see above), we are actually creating a post request by posting the json data that we creared in line#16.

Finally, in line#21, we are printng the content

Let us execute.​​ 

Notice that​​ the new user gets posted and created in the server and a unique ‘id’ is assigned to this new user at runtime

Let us also print the status code(line#22)

Execute and notice below that status code 201 (successful post) is returned from the server side

Let us comment line#16 and add lines#17 and 18. So basically, this time, we are posting the user’s ‘job’ as well

Execute

POST Request (Register successful)

Let us now see another example of a POST request.​​ 

Below we can see that, in the response, we get​​ a token

Let us change the end point and post the data as shown below. We have used the same email id and password seen above

Save and run.

Observe below that token gets generated (that means user registration was successful) and our test passes

Code​​ snippet (post data)

import​​ requests

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect

def​​ run(playwright: Playwright) ->​​ None:

 ​​ ​​ ​​​​ #resp = requests.get("https://reqres.in/api/users/")

 ​​ ​​ ​​​​ #id = input("Which user id should i get for you? ")
 ​​ ​​ ​​​​ #resp = requests.get(f"https://reqres.in/api/users/{id}")

 ​​ ​​ ​​​​ #print(resp.json())

 ​​ ​​ ​​​​ #Post
 ​​ ​​ ​​​​ name =​​ input("What should be the name of new user? ")
 ​​ ​​ ​​​​ #user = {"name": name}
 ​​ ​​ ​​​​ job =​​ input("What should be the job of new user? ")
 ​​ ​​ ​​​​ user =​​ {"name": name,​​ "job": job}

 ​​ ​​ ​​​​ headers = {"Content-type":​​ "application/json"}
 ​​ ​​ ​​​​ resp = requests.post("https://reqres.in/api/users",​​ json=user,​​ headers=headers)
 ​​ ​​ ​​​​ print(resp.content)
 ​​ ​​ ​​​​ print(resp.status_code)
 ​​ ​​ ​​​​ 
with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Code snippet ​​ (Register successful)

import​​ requests

from​​ playwright.sync_api​​ import​​ Playwright,​​ sync_playwright,​​ expect

def​​ run(playwright: Playwright) ->​​ None:

 ​​ ​​ ​​​​ #resp = requests.get("https://reqres.in/api/users/")

 ​​ ​​ ​​​​ #id =​​ input("Which user id should i get for you? ")
 ​​ ​​ ​​​​ #resp = requests.get(f"https://reqres.in/api/users/{id}")

 ​​ ​​ ​​​​ #print(resp.json())

 ​​ ​​ ​​​​ #Post
 ​​ ​​ ​​​​ email =​​ "eve.holt@reqres.in"
 ​​ ​​ ​​​​ password =​​ "cityslicka"
 ​​ ​​ ​​​​ user = {"email": email,​​ "password": password}

 ​​ ​​ ​​​​ headers = {"Content-type":​​ "application/json"}
 ​​ ​​ ​​​​ resp = requests.post("https://reqres.in/api/login",​​ json=user,​​ headers=headers)
 ​​ ​​ ​​​​ print(resp.content)
 ​​ ​​ ​​​​ print(resp.status_code)
 ​​ ​​ ​​​​ 
with​​ sync_playwright()​​ as​​ playwright:
 ​​ ​​ ​​​​ run(playwright)

 

Thank​​ you for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024