{"id":3232,"date":"2025-01-16T03:54:15","date_gmt":"2025-01-16T03:54:15","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=3232"},"modified":"2025-01-17T10:37:44","modified_gmt":"2025-01-17T10:37:44","slug":"create-and-validate-post-request-using-playwright-java","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/create-and-validate-post-request-using-playwright-java\/","title":{"rendered":"Create and validate POST request using Playwright Java\u00a0\u00a0"},"content":{"rendered":"<p><em>In this blog we will learn about POST Request (API Testing) using Playwright Java.<\/em><\/p>\n<p><strong>Topics that we will cover:<\/strong><\/p>\n<ul>\n<li>Create a Post Request using playwright<\/li>\n<li>Learn how to parse json<\/li>\n<li>Sample Post Request (Register successful, 200)<\/li>\n<li>Sample Post Request (Login unsuccessful, 400)<\/li>\n<li>Source code (Post a new user and parse json)<\/li>\n<li>Source code (register successful)<\/li>\n<li>Source code (login unsuccessful)<\/li>\n<\/ul>\n<p><strong>Create a Post Request using playwright<\/strong><\/p>\n<p>In this section, we will explore the process of making an API request using Playwright through the POST method.<\/p>\n<p>Go to <a href=\"https:\/\/reqres.in\/\" target=\"_blank\" rel=\"noopener\">https:\/\/reqres.in\/<\/a>&nbsp;<\/p>\n<p>Please note that for the POST request, it is necessary to provide a body as part of the request, see below&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3235\" width=\"615\" data-init-width=\"1182\" height=\"57\" data-init-height=\"110\" title=\"Screenshot 2025-01-16 at 9.26.16\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.26.16\u202fAM.png\" data-width=\"615\" data-height=\"57\" style=\"aspect-ratio: auto 1182 \/ 110;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3236\" width=\"507\" data-init-width=\"1182\" height=\"248\" data-init-height=\"578\" title=\"Screenshot 2025-01-16 at 9.27.11\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.27.11\u202fAM.png\" data-width=\"507\" data-height=\"248\" style=\"aspect-ratio: auto 1182 \/ 578;\"><\/span><\/p>\n<p>Let us explore the process of creating a new user ID using Playwright Java.<\/p>\n<p>To begin with, create an object of \u2018<em>APIRequestContext<\/em>\u2019<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3237\" width=\"573\" data-init-width=\"1182\" height=\"96\" data-init-height=\"198\" title=\"Screenshot 2025-01-16 at 9.28.25\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.28.25\u202fAM.png\" data-width=\"573\" data-height=\"96\" style=\"aspect-ratio: auto 1182 \/ 198;\"><\/span><\/p>\n<p>&nbsp;Using Map create body of POST request.&nbsp;<\/p>\n<p>The data that we intend to send as part of the POST request will be included in the body<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3238\" width=\"560\" data-init-width=\"1182\" height=\"94\" data-init-height=\"198\" title=\"Screenshot 2025-01-16 at 9.29.00\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.29.00\u202fAM.png\" data-width=\"560\" data-height=\"94\" style=\"aspect-ratio: auto 1182 \/ 198;\"><\/span><\/p>\n<p>&nbsp;After that, we will obtain the text response and assign it to a string variable.&nbsp;<\/p>\n<p>The post() method provided below takes in two parameters: the first parameter represents the uri where we intend to post our data, while the second parameter represents the data itself, which we have previously created<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3239\" width=\"560\" data-init-width=\"1182\" height=\"136\" data-init-height=\"288\" title=\"Screenshot 2025-01-16 at 9.40.21\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.40.21\u202fAM.png\" data-width=\"560\" data-height=\"136\" style=\"aspect-ratio: auto 1182 \/ 288;\"><\/span><\/p>\n<p>&nbsp;At the end, we will print the response in the console&nbsp;<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3240\" width=\"561\" data-init-width=\"1182\" height=\"219\" data-init-height=\"462\" title=\"Screenshot 2025-01-16 at 9.40.57\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.40.57\u202fAM.png\" data-width=\"561\" data-height=\"219\" style=\"aspect-ratio: auto 1182 \/ 462;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>POST request passes. New user \u2018id\u2019 is created and printed<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3241\" width=\"583\" data-init-width=\"1182\" height=\"85\" data-init-height=\"172\" title=\"Screenshot 2025-01-16 at 9.41.40\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.41.40\u202fAM.png\" data-width=\"583\" data-height=\"85\" style=\"aspect-ratio: auto 1182 \/ 172;\"><\/span><\/p>\n<p>&nbsp;<strong>Learn how to parse json<\/strong><\/p>\n<p>json parser \u2018gson\u2019 can be used to parse json https:\/\/mvnrepository.com\/artifact\/com.google.code.gson\/gson\/2.10.1<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3242\" width=\"546\" data-init-width=\"1182\" height=\"498\" data-init-height=\"1078\" title=\"Screenshot 2025-01-16 at 9.42.23\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.42.23\u202fAM.png\" data-width=\"546\" data-height=\"498\" style=\"aspect-ratio: auto 1182 \/ 1078;\"><\/span><\/p>\n<p>&nbsp; Add dependency in xml<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3243\" width=\"600\" data-init-width=\"1182\" height=\"155\" data-init-height=\"306\" title=\"Screenshot 2025-01-16 at 9.43.02\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.43.02\u202fAM.png\" data-width=\"600\" data-height=\"155\" style=\"aspect-ratio: auto 1182 \/ 306;\"><\/span><\/p>\n<p>&nbsp;Save pom.xml<\/p>\n<p>An instance of Gson is created and the response is passed as an argument to the <em>fromJson()<\/em> method<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3244\" width=\"627\" data-init-width=\"1182\" height=\"178\" data-init-height=\"336\" title=\"Screenshot 2025-01-16 at 9.43.37\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.43.37\u202fAM.png\" data-width=\"627\" data-height=\"178\" style=\"aspect-ratio: auto 1182 \/ 336;\"><\/span><\/p>\n<p>&nbsp;So let us parse id, name, job<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3245\" width=\"590\" data-init-width=\"1182\" height=\"160\" data-init-height=\"320\" title=\"Screenshot 2025-01-16 at 9.44.20\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.44.20\u202fAM.png\" data-width=\"590\" data-height=\"160\" style=\"aspect-ratio: auto 1182 \/ 320;\"><\/span><\/p>\n<p>&nbsp;Code so far is as below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3246\" width=\"568\" data-init-width=\"1182\" height=\"279\" data-init-height=\"580\" title=\"Screenshot 2025-01-16 at 9.44.53\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.44.53\u202fAM.png\" data-width=\"568\" data-height=\"279\" style=\"aspect-ratio: auto 1182 \/ 580;\"><\/span><\/p>\n<p>&nbsp;Execute the code.&nbsp;<\/p>\n<p>id, name, job gets printed<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3247\" width=\"559\" data-init-width=\"1182\" height=\"169\" data-init-height=\"358\" title=\"Screenshot 2025-01-16 at 9.45.26\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.45.26\u202fAM.png\" data-width=\"559\" data-height=\"169\" style=\"aspect-ratio: auto 1182 \/ 358;\"><\/span><\/p>\n<p>&nbsp;<strong>Sample Post Request (Register successful, 200)<\/strong><\/p>\n<p>Next, we will explore another example of a POST request.&nbsp;<\/p>\n<p>It can be observed in the response below that a token is received<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3248\" width=\"578\" data-init-width=\"1182\" height=\"65\" data-init-height=\"132\" title=\"Screenshot 2025-01-16 at 9.45.58\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.45.58\u202fAM.png\" data-width=\"578\" data-height=\"65\" style=\"aspect-ratio: auto 1182 \/ 132;\"><\/span><span><img decoding=\"async\" alt=\"\" data-id=\"3249\" width=\"605\" data-init-width=\"1182\" height=\"227\" data-init-height=\"444\" title=\"Screenshot 2025-01-16 at 9.46.39\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.46.39\u202fAM.png\" data-width=\"605\" data-height=\"227\" style=\"aspect-ratio: auto 1182 \/ 444;\"><\/span><\/p>\n<p>&nbsp;Modify the endpoint and post the data in the following manner. We have utilized the identical email id and password as mentioned above<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3250\" width=\"618\" data-init-width=\"1182\" height=\"246\" data-init-height=\"470\" title=\"Screenshot 2025-01-16 at 9.47.10\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.47.10\u202fAM.png\" data-width=\"618\" data-height=\"246\" style=\"aspect-ratio: auto 1182 \/ 470;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>The generation of a token indicates that the user registration process was successful. Also the \u2018id\u2019 gets generated, see below<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3251\" width=\"658\" data-init-width=\"1182\" height=\"203\" data-init-height=\"364\" title=\"Screenshot 2025-01-16 at 9.47.47\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.47.47\u202fAM.png\" data-width=\"658\" data-height=\"203\" style=\"aspect-ratio: auto 1182 \/ 364;\"><\/span><\/p>\n<p>&nbsp;<strong>Sample Post Request (Login unsuccessful, 400)<\/strong><\/p>\n<p>Now, let&#8217;s analyze the last scenario<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3252\" width=\"568\" data-init-width=\"1182\" height=\"276\" data-init-height=\"574\" title=\"Screenshot 2025-01-16 at 9.48.30\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.48.30\u202fAM.png\" data-width=\"568\" data-height=\"276\" style=\"aspect-ratio: auto 1182 \/ 574;\"><\/span><\/p>\n<p>&nbsp;Change uri plus other details<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3253\" width=\"569\" data-init-width=\"1182\" height=\"214\" data-init-height=\"444\" title=\"Screenshot 2025-01-16 at 9.49.00\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.49.00\u202fAM.png\" data-width=\"569\" data-height=\"214\" style=\"aspect-ratio: auto 1182 \/ 444;\"><\/span><\/p>\n<p>&nbsp;Execute.<\/p>\n<p>The console prints error<\/p>\n<p><span><img decoding=\"async\" alt=\"\" data-id=\"3254\" width=\"586\" data-init-width=\"1182\" height=\"106\" data-init-height=\"214\" title=\"Screenshot 2025-01-16 at 9.49.40\u202fAM\" loading=\"lazy\" src=\"https:\/\/rahulshettyacademy.com\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.49.40\u202fAM.png\" data-width=\"586\" data-height=\"106\" style=\"aspect-ratio: auto 1182 \/ 214;\"><\/span><\/p>\n<p>&nbsp;You can try out POST requests in a similar fashion using playwright java.&nbsp;<\/p>\n<p><strong>Source code (Post a new user and parse json)<\/strong><\/p>\n<p><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> java.util.HashMap;<strong>import<\/strong> java.util.Map;<strong>import<\/strong> com.google.gson.Gson;<strong>import<\/strong> com.google.gson.JsonObject;<strong>import<\/strong> com.microsoft.playwright.APIRequest;<strong>import<\/strong> com.microsoft.playwright.APIRequestContext;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>import<\/strong> com.microsoft.playwright.options.RequestOptions;<strong>public<\/strong><strong>class<\/strong> Blog14_PostNewUser {<strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {Playwright pw = Playwright.<em>create<\/em>();APIRequestContext arc = pw.request().newContext(<strong>new<\/strong> APIRequest.NewContextOptions());Map&lt;String, String&gt; mdata = <strong>new<\/strong> HashMap&lt;&gt;();mdata.put(&#8220;name&#8221;, &#8220;RahulShetty&#8221;);mdata.put(&#8220;role&#8221;, &#8220;SDET&#8221;);String resp = arc.post(&#8220;https:\/\/reqres.in\/api\/users&#8221;,RequestOptions.<em>create<\/em>().setData(mdata)).text();System.<strong><em>out<\/em><\/strong>.println(&#8220;Response &#8211;&gt;&#8221; + resp);\/\/parse json dataJsonObject jobj = <strong>new<\/strong> Gson().fromJson(resp, JsonObject.<strong>class<\/strong>);System.<strong><em>out<\/em><\/strong>.println(&#8220;name is &#8211;&gt;&#8221; + jobj.get(&#8220;name&#8221;));System.<strong><em>out<\/em><\/strong>.println(&#8220;job is &#8211;&gt;&#8221; + jobj.get(&#8220;role&#8221;));System.<strong><em>out<\/em><\/strong>.println(&#8220;Id is &#8211;&gt;&#8221; + jobj.get(&#8220;id&#8221;));}}<strong>Source code (register successful)<\/strong><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> java.util.HashMap;<strong>import<\/strong> java.util.Map;<strong>import<\/strong> com.google.gson.Gson;<strong>import<\/strong> com.google.gson.JsonObject;<strong>import<\/strong> com.microsoft.playwright.APIRequest;<strong>import<\/strong> com.microsoft.playwright.APIRequestContext;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>import<\/strong> com.microsoft.playwright.options.RequestOptions;<strong>public<\/strong><strong>class<\/strong> Blog14_RegisterSuccessful {<strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {Playwright pw = Playwright.<em>create<\/em>();APIRequestContext arc = pw.request().newContext(<strong>new<\/strong> APIRequest.NewContextOptions());Map&lt;String, String&gt; mdata = <strong>new<\/strong> HashMap&lt;&gt;();mdata.put(&#8220;email&#8221;, &#8220;eve.holt@reqres.in&#8221;);mdata.put(&#8220;password&#8221;, &#8220;pistol&#8221;);String resp = arc.post(&#8220;https:\/\/reqres.in\/api\/register&#8221;,RequestOptions.<em>create<\/em>().setData(mdata)).text();System.<strong><em>out<\/em><\/strong>.println(&#8220;Response &#8211;&gt;&#8221; + resp);\/\/parse json dataJsonObject jobj = <strong>new<\/strong> Gson().fromJson(resp, JsonObject.<strong>class<\/strong>);System.<strong><em>out<\/em><\/strong>.println(&#8220;token is &#8211;&gt;&#8221; + jobj.get(&#8220;token&#8221;));System.<strong><em>out<\/em><\/strong>.println(&#8220;Id is &#8211;&gt;&#8221; + jobj.get(&#8220;id&#8221;));}}<strong>Source code (login unsuccessful)<\/strong><strong>package<\/strong> com.rsa.playwrightjava;<strong>import<\/strong> java.util.HashMap;<strong>import<\/strong> java.util.Map;<strong>import<\/strong> com.google.gson.Gson;<strong>import<\/strong> com.google.gson.JsonObject;<strong>import<\/strong> com.microsoft.playwright.APIRequest;<strong>import<\/strong> com.microsoft.playwright.APIRequestContext;<strong>import<\/strong> com.microsoft.playwright.Playwright;<strong>import<\/strong> com.microsoft.playwright.options.RequestOptions;<strong>public<\/strong><strong>class<\/strong> Blog14_LoginUnsuccessful {<strong>public<\/strong><strong>static<\/strong><strong>void<\/strong> main(String[] args) {Playwright pw = Playwright.<em>create<\/em>();APIRequestContext arc = pw.request().newContext(<strong>new<\/strong> APIRequest.NewContextOptions());Map&lt;String, String&gt; mdata = <strong>new<\/strong> HashMap&lt;&gt;();mdata.put(&#8220;email&#8221;, &#8220;peter@klaven&#8221;);String resp = arc.post(&#8220;https:\/\/reqres.in\/api\/login&#8221;,RequestOptions.<em>create<\/em>().setData(mdata)).text();System.<strong><em>out<\/em><\/strong>.println(&#8220;Response &#8211;&gt;&#8221; + resp);\/\/parse json dataJsonObject jobj = <strong>new<\/strong> Gson().fromJson(resp, JsonObject.<strong>class<\/strong>);System.<strong><em>out<\/em><\/strong>.println(&#8220;error is &#8211;&gt;&#8221; + jobj.get(&#8220;error&#8221;));}}Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog we will learn about POST Request (API Testing) using Playwright Java. Topics that we will cover: Create a Post Request using playwright Learn how to parse json Sample Post Request (Register successful, 200) Sample Post Request (Login unsuccessful, 400) Source code (Post a new user and parse json) Source code (register successful) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":750,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-3232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-playwright","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3232"}],"version-history":[{"count":6,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3232\/revisions"}],"predecessor-version":[{"id":3713,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3232\/revisions\/3713"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/750"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}