Tutorial 17 – Post API Request using Playwright Java
What you will Learn in this blog:
Post Request API
Parse json body
POST Request (Register successful)
POST Request (Register unsuccessful)
Code snippets
Post Request API
Let us now see how to POST an API request using PW.
Launch https://reqres.in/
Notice below that we need to pass some body as part of POST request (unlike a GET request)
So let us see how to create or POST a new user id using Playwright.
We start with creating an object of ‘APIRequestContext’ as shown below
We than create a body of POST request by creating a Map. The body will contain the data that we want to send as part of POST request
Next, we will fetch the text response in a string variable. The post() method shown below accepts 2 arguments: first argument is the uri where we want to post our data and the second argument is the data itself (that we created above)
Finally we will print the response (line#23).
Our code looks like below
Save and execute.
Notice that POST request is successful and new user ‘id’ got created and gets printed in console
Parse json body
To parse the json body, we will use a json parser ‘gson’ https://mvnrepository.com/artifact/com.google.code.gson/gson/2.10
Copy the dependency and paste it in pom.xml
Save the xml.
We now create an instance of gson and pass the response as an argument to fromJson() method
Finally, we can start parsing the json body. Let us parse the ‘id’
Complete code looks like below
Save and execute.
Observe below that the id gets printed
Similarly we can parse the name and job
We can also parse the entire body
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
POST Request (Register unsuccessful)
Let us take last scenario. We have an error text in the response data
Change the endpoint and post ‘email’ data
Save and run.
Observe below that we get the desired error
This is how we can work with POST API.
Refer the url for any further reference
https://playwright.dev/java/docs/api/class-apirequestcontext
Code snippet (POST Request to create new user)
package com.w2a.pwjava;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.RequestOptions;
public class PostAPIReqPW {
public static void main(String[] args) {
Playwright pw = Playwright.create();
APIRequestContext apiRequestContext = pw.request()
.newContext(new APIRequest.NewContextOptions());
Map<String, String> data = new HashMap<>();
data.put("name", "playwrightuser");
data.put("job", "w2a");
String response = apiRequestContext.post("https://reqres.in/api/users",RequestOptions.create().setData(data)).text();
System.out.println("Server response-->" + response);
//parse json
JsonObject jo = new Gson().fromJson(response, JsonObject.class);
System.out.println(jo.get("id"));
System.out.println(jo.get("name"));
System.out.println(jo.get("job"));
}
}
Code snippet (POST request - registration successful)
package com.w2a.pwjava;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.RequestOptions;
public class PostAPIRegistrationReqPW {
public static void main(String[] args) {
Playwright pw = Playwright.create();
APIRequestContext apiRequestContext = pw.request()
.newContext(new APIRequest.NewContextOptions());
Map<String, String> data = new HashMap<>();
data.put("email", "eve.holt@reqres.in");
data.put("password", "cityslicka");
String response = apiRequestContext.post("https://reqres.in/api/login",RequestOptions.create().setData(data)).text();
System.out.println("Server response-->" + response);
//parse json
JsonObject jo = new Gson().fromJson(response, JsonObject.class);
System.out.println(jo.get("token"));
}
}
Code snippet (for registration unsuccessful)
package com.w2a.pwjava;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.microsoft.playwright.APIRequest;
import com.microsoft.playwright.APIRequestContext;
import com.microsoft.playwright.Playwright;
import com.microsoft.playwright.options.RequestOptions;
public class PostAPIRegistrationFailReqPW {
public static void main(String[] args) {
Playwright pw = Playwright.create();
APIRequestContext apiRequestContext = pw.request()
.newContext(new APIRequest.NewContextOptions());
Map<String, String> data = new HashMap<>();
data.put("email", "sydney@fife");
String response = apiRequestContext.post("https://reqres.in/api/register",RequestOptions.create().setData(data)).text();
System.out.println("Server response-->" + response);
//parse json
JsonObject jo = new Gson().fromJson(response, JsonObject.class);
System.out.println(jo.get("error"));
}
}
Thank you for reading!