{"id":3232,"date":"2025-01-16T03:54:15","date_gmt":"2025-01-16T03:54:15","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2025\/01\/16\/create-and-validate-post-request-using-playwright-java\/"},"modified":"2025-01-16T03:54:15","modified_gmt":"2025-01-16T03:54:15","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"},"content":{"rendered":"<p><html><body><\/p>\n<section class=\"tcb-post-content tcb-shortcode thrv_wrapper\" data-css=\"tve-u-16ec5d248bb\">\n<div class=\"tve_flt tcb-style-wrap\" id=\"tve_flt\">\n<div class=\"tve_shortcode_editor tar-main-content\" data-post-id=\"3232\" id=\"tve_editor\">\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><em>In this blog we will learn about POST Request (API Testing) using Playwright Java.<\/em><\/p>\n<div class=\"code-block code-block-2\" style=\"margin: 8px auto; text-align: center; display: block; clear: both;\">\n<p><!-- horizontal responsive 2 --><br \/>\n<ins class=\"adsbygoogle\" data-ad-client=\"ca-pub-3231318496783163\" data-ad-format=\"auto\" data-ad-slot=\"2788468763\" data-full-width-responsive=\"true\" style=\"display:block\"><\/ins>\n<\/div>\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\/\" rel=\"noopener\" target=\"_blank\">https:\/\/reqres.in\/<\/a>\u00a0<\/p>\n<p>Please note that for the POST request, it is necessary to provide a body as part of the request, see below\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d42e7b0\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3235\" data-css=\"tve-u-1946d42fa76\" data-height=\"57\" data-id=\"3235\" data-init-height=\"110\" data-init-width=\"1182\" data-width=\"615\" decoding=\"async\" height=\"57\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.26.16\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 110;\" title=\"Screenshot 2025-01-16 at 9.26.16\u202fAM\" width=\"615\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d43d205\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3236\" data-css=\"tve-u-1946d43def4\" data-height=\"248\" data-id=\"3236\" data-init-height=\"578\" data-init-width=\"1182\" data-width=\"507\" decoding=\"async\" height=\"248\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.27.11\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 578;\" title=\"Screenshot 2025-01-16 at 9.27.11\u202fAM\" width=\"507\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d44e8b4\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3237\" data-css=\"tve-u-1946d44fe60\" data-height=\"96\" data-id=\"3237\" data-init-height=\"198\" data-init-width=\"1182\" data-width=\"573\" decoding=\"async\" height=\"96\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.28.25\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 198;\" title=\"Screenshot 2025-01-16 at 9.28.25\u202fAM\" width=\"573\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Using Map create body of POST request.\u00a0<\/p>\n<p>The data that we intend to send as part of the POST request will be included in the body<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d456107\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3238\" data-css=\"tve-u-1946d457026\" data-height=\"94\" data-id=\"3238\" data-init-height=\"198\" data-init-width=\"1182\" data-width=\"560\" decoding=\"async\" height=\"94\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.29.00\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 198;\" title=\"Screenshot 2025-01-16 at 9.29.00\u202fAM\" width=\"560\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0After that, we will obtain the text response and assign it to a string variable.\u00a0<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d4fdb03\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3239\" data-css=\"tve-u-1946d4fecb5\" data-height=\"136\" data-id=\"3239\" data-init-height=\"288\" data-init-width=\"1182\" data-width=\"560\" decoding=\"async\" height=\"136\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.40.21\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 288;\" title=\"Screenshot 2025-01-16 at 9.40.21\u202fAM\" width=\"560\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0At the end, we will print the response in the console\u00a0<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d506a21\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3240\" data-css=\"tve-u-1946d507b80\" data-height=\"219\" data-id=\"3240\" data-init-height=\"462\" data-init-width=\"1182\" data-width=\"561\" decoding=\"async\" height=\"219\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.40.57\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 462;\" title=\"Screenshot 2025-01-16 at 9.40.57\u202fAM\" width=\"561\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>POST request passes. New user \u2018id\u2019 is created and printed<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d50fe59\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3241\" data-css=\"tve-u-1946d5117b2\" data-height=\"85\" data-id=\"3241\" data-init-height=\"172\" data-init-width=\"1182\" data-width=\"583\" decoding=\"async\" height=\"85\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.41.40\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 172;\" title=\"Screenshot 2025-01-16 at 9.41.40\u202fAM\" width=\"583\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d51b46d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3242\" data-css=\"tve-u-1946d51c5b6\" data-height=\"498\" data-id=\"3242\" data-init-height=\"1078\" data-init-width=\"1182\" data-width=\"546\" decoding=\"async\" height=\"498\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.42.23\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 1078;\" title=\"Screenshot 2025-01-16 at 9.42.23\u202fAM\" width=\"546\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0 Add dependency in xml<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d52434d\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3243\" data-css=\"tve-u-1946d525735\" data-height=\"155\" data-id=\"3243\" data-init-height=\"306\" data-init-width=\"1182\" data-width=\"600\" decoding=\"async\" height=\"155\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.43.02\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 306;\" title=\"Screenshot 2025-01-16 at 9.43.02\u202fAM\" width=\"600\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Save 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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d52c654\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3244\" data-css=\"tve-u-1946d52da5c\" data-height=\"178\" data-id=\"3244\" data-init-height=\"336\" data-init-width=\"1182\" data-width=\"627\" decoding=\"async\" height=\"178\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.43.37\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 336;\" title=\"Screenshot 2025-01-16 at 9.43.37\u202fAM\" width=\"627\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0So let us parse id, name, job<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d537b70\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3245\" data-css=\"tve-u-1946d538d16\" data-height=\"160\" data-id=\"3245\" data-init-height=\"320\" data-init-width=\"1182\" data-width=\"590\" decoding=\"async\" height=\"160\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.44.20\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 320;\" title=\"Screenshot 2025-01-16 at 9.44.20\u202fAM\" width=\"590\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Code so far is as below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d53eaf8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3246\" data-css=\"tve-u-1946d53feb0\" data-height=\"279\" data-id=\"3246\" data-init-height=\"580\" data-init-width=\"1182\" data-width=\"568\" decoding=\"async\" height=\"279\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.44.53\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 580;\" title=\"Screenshot 2025-01-16 at 9.44.53\u202fAM\" width=\"568\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute the code.\u00a0<\/p>\n<p>id, name, job gets printed<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d54700c\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3247\" data-css=\"tve-u-1946d54827e\" data-height=\"169\" data-id=\"3247\" data-init-height=\"358\" data-init-width=\"1182\" data-width=\"559\" decoding=\"async\" height=\"169\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.45.26\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 358;\" title=\"Screenshot 2025-01-16 at 9.45.26\u202fAM\" width=\"559\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Sample Post Request (Register successful, 200)<\/strong><\/p>\n<p>Next, we will explore another example of a POST request.\u00a0<\/p>\n<p>It can be observed in the response below that a token is received<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d54e87a\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3248\" data-css=\"tve-u-1946d54ffc1\" data-height=\"65\" data-id=\"3248\" data-init-height=\"132\" data-init-width=\"1182\" data-width=\"578\" decoding=\"async\" height=\"65\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.45.58\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 132;\" title=\"Screenshot 2025-01-16 at 9.45.58\u202fAM\" width=\"578\"\/><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d55878e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3249\" data-css=\"tve-u-1946d559d83\" data-height=\"227\" data-id=\"3249\" data-init-height=\"444\" data-init-width=\"1182\" data-width=\"605\" decoding=\"async\" height=\"227\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.46.39\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 444;\" title=\"Screenshot 2025-01-16 at 9.46.39\u202fAM\" width=\"605\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Modify the endpoint and post the data in the following manner. We have utilized the identical email id and password as mentioned above<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d5600e0\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3250\" data-css=\"tve-u-1946d56171c\" data-height=\"246\" data-id=\"3250\" data-init-height=\"470\" data-init-width=\"1182\" data-width=\"618\" decoding=\"async\" height=\"246\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.47.10\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 470;\" title=\"Screenshot 2025-01-16 at 9.47.10\u202fAM\" width=\"618\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/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<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d569143\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3251\" data-css=\"tve-u-1946d56a638\" data-height=\"203\" data-id=\"3251\" data-init-height=\"364\" data-init-width=\"1182\" data-width=\"658\" decoding=\"async\" height=\"203\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.47.47\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 364;\" title=\"Screenshot 2025-01-16 at 9.47.47\u202fAM\" width=\"658\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0<strong>Sample Post Request (Login unsuccessful, 400)<\/strong><\/p>\n<p>Now, let&#8217;s analyze the last scenario<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d574013\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3252\" data-css=\"tve-u-1946d574b8b\" data-height=\"276\" data-id=\"3252\" data-init-height=\"574\" data-init-width=\"1182\" data-width=\"568\" decoding=\"async\" height=\"276\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.48.30\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 574;\" title=\"Screenshot 2025-01-16 at 9.48.30\u202fAM\" width=\"568\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Change uri plus other details<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d57c6a8\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3253\" data-css=\"tve-u-1946d57e5f6\" data-height=\"214\" data-id=\"3253\" data-init-height=\"444\" data-init-width=\"1182\" data-width=\"569\" decoding=\"async\" height=\"214\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.49.00\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 444;\" title=\"Screenshot 2025-01-16 at 9.49.00\u202fAM\" width=\"569\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0Execute.<\/p>\n<p>The console prints error<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-1946d58513e\" style=\"\"><span class=\"tve_image_frame\"><img alt=\"\" class=\"tve_image wp-image-3254\" data-css=\"tve-u-1946d5863bb\" data-height=\"106\" data-id=\"3254\" data-init-height=\"214\" data-init-width=\"1182\" data-width=\"586\" decoding=\"async\" height=\"106\" loading=\"lazy\" src=\"\/blog\/wp-content\/uploads\/2025\/01\/Screenshot-2025-01-16-at-9.49.40\u202fAM.png\" style=\"aspect-ratio: auto 1182 \/ 214;\" title=\"Screenshot 2025-01-16 at 9.49.40\u202fAM\" width=\"586\"\/><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>\u00a0You can try out POST requests in a similar fashion using playwright java.\u00a0<\/p>\n<p><strong>Source code (Post a new user and parse json)<\/strong><\/p>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> java.util.HashMap;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> java.util.Map;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.google.gson.Gson;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.google.gson.JsonObject;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.APIRequest;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.APIRequestContext;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.options.RequestOptions;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog14_PostNewUser {<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/div>\n<div class=\"tcb-plain-text\">Playwright pw = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\">APIRequestContext arc = pw.request().newContext(<strong>new<\/strong> APIRequest.NewContextOptions());<\/div>\n<div class=\"tcb-plain-text\">Map&lt;String, String&gt; mdata = <strong>new<\/strong> HashMap&lt;&gt;();<\/div>\n<div class=\"tcb-plain-text\">mdata.put(&#8220;name&#8221;, &#8220;RahulShetty&#8221;);<\/div>\n<div class=\"tcb-plain-text\">mdata.put(&#8220;role&#8221;, &#8220;SDET&#8221;);<\/div>\n<div class=\"tcb-plain-text\">String resp = arc.post(&#8220;https:\/\/reqres.in\/api\/users&#8221;,RequestOptions.<em>create<\/em>().setData(mdata)).text();<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;Response &#8211;&gt;&#8221; + resp);<\/div>\n<div class=\"tcb-plain-text\">\/\/parse json data<\/div>\n<div class=\"tcb-plain-text\">JsonObject jobj = <strong>new<\/strong> Gson().fromJson(resp, JsonObject.<strong>class<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;name is &#8211;&gt;&#8221; + jobj.get(&#8220;name&#8221;));<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;job is &#8211;&gt;&#8221; + jobj.get(&#8220;role&#8221;));<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;Id is &#8211;&gt;&#8221; + jobj.get(&#8220;id&#8221;));<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>Source code (register successful)<\/strong><\/div>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> java.util.HashMap;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> java.util.Map;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.google.gson.Gson;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.google.gson.JsonObject;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.APIRequest;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.APIRequestContext;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.options.RequestOptions;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog14_RegisterSuccessful {<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/div>\n<div class=\"tcb-plain-text\">Playwright pw = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\">APIRequestContext arc = pw.request().newContext(<strong>new<\/strong> APIRequest.NewContextOptions());<\/div>\n<div class=\"tcb-plain-text\">Map&lt;String, String&gt; mdata = <strong>new<\/strong> HashMap&lt;&gt;();<\/div>\n<div class=\"tcb-plain-text\">mdata.put(&#8220;email&#8221;, &#8220;eve.holt@reqres.in&#8221;);<\/div>\n<div class=\"tcb-plain-text\">mdata.put(&#8220;password&#8221;, &#8220;pistol&#8221;);<\/div>\n<div class=\"tcb-plain-text\">String resp = arc.post(&#8220;https:\/\/reqres.in\/api\/register&#8221;,RequestOptions.<em>create<\/em>().<\/div>\n<div class=\"tcb-plain-text\">setData(mdata)).text();<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;Response &#8211;&gt;&#8221; + resp);<\/div>\n<div class=\"tcb-plain-text\">\/\/parse json data<\/div>\n<div class=\"tcb-plain-text\">JsonObject jobj = <strong>new<\/strong> Gson().fromJson(resp, JsonObject.<strong>class<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;token is &#8211;&gt;&#8221; + jobj.get(&#8220;token&#8221;));<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;Id is &#8211;&gt;&#8221; + jobj.get(&#8220;id&#8221;));<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>Source code (login unsuccessful)<\/strong><\/div>\n<div class=\"tcb-plain-text\"><strong>package<\/strong> com.rsa.playwrightjava;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> java.util.HashMap;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> java.util.Map;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.google.gson.Gson;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.google.gson.JsonObject;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.APIRequest;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.APIRequestContext;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.Playwright;<\/div>\n<div class=\"tcb-plain-text\"><strong>import<\/strong> com.microsoft.playwright.options.RequestOptions;<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>class<\/strong> Blog14_LoginUnsuccessful {<\/div>\n<div class=\"tcb-plain-text\"><strong>public<\/strong><br \/>\n<strong>static<\/strong><br \/>\n<strong>void<\/strong> main(String[] args) {<\/div>\n<div class=\"tcb-plain-text\">Playwright pw = Playwright.<em>create<\/em>();<\/div>\n<div class=\"tcb-plain-text\">APIRequestContext arc = pw.request().newContext(<strong>new<\/strong> APIRequest.NewContextOptions());<\/div>\n<div class=\"tcb-plain-text\">Map&lt;String, String&gt; mdata = <strong>new<\/strong> HashMap&lt;&gt;();<\/div>\n<div class=\"tcb-plain-text\">mdata.put(&#8220;email&#8221;, &#8220;peter@klaven&#8221;);<\/div>\n<div class=\"tcb-plain-text\">String resp = arc.post(&#8220;https:\/\/reqres.in\/api\/login&#8221;,RequestOptions.<em>create<\/em>().<\/div>\n<div class=\"tcb-plain-text\">setData(mdata)).text();<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;Response &#8211;&gt;&#8221; + resp);<\/div>\n<div class=\"tcb-plain-text\">\/\/parse json data<\/div>\n<div class=\"tcb-plain-text\">JsonObject jobj = <strong>new<\/strong> Gson().fromJson(resp, JsonObject.<strong>class<\/strong>);<\/div>\n<div class=\"tcb-plain-text\">System.<strong><em>out<\/em><\/strong>.println(&#8220;error is &#8211;&gt;&#8221; + jobj.get(&#8220;error&#8221;));<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\">}<\/div>\n<div class=\"tcb-plain-text\"><\/div>\n<div class=\"tcb-plain-text\">Thanks.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb_flag\" style=\"display: none\"><\/div>\n<\/section>\n<p><\/body><\/html><\/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":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,9],"tags":[],"class_list":["post-3232","post","type-post","status-publish","format-standard","hentry","category-java","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":0,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3232\/revisions"}],"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}]}}