What is PUT API?
- A PUT API is an HTTP request method used in RESTful web services to update an existing resource on the server.
- Unlike POST, which is used to create new data, PUT is typically used when the client wants to replace or update the entire resource with new data.
- Example use case: Updating a user’s profile information (like email, phone number, or address).
Important Characteristics:
- Idempotent → Multiple identical PUT requests should result in the same state of the resource.
- Requires a complete representation of the resource to be sent in the request body (not just partial changes, unless API supports PATCH).
Testing a PUT API with Playwright Java
Although Playwright is mainly used for UI automation, it also provides powerful API testing capabilities. Using Playwright with Java, you can send PUT requests and validate the responses.
(1) Create a request context
- In Playwright, you use a APIRequestContext (or similar request handling object in Java binding) to interact with APIs.
- This context acts like a lightweight HTTP client.
(2) Send a PUT request
- Use the request context to call the PUT method on a given API endpoint.
- Provide necessary details like:
Headers → Usually includes Content-Type: application/json, and sometimes Authorization tokens.
Request body → JSON data with updated information for the resource.
(3) Validate the Response
- Check the status code (e.g., 200 OK or 204 No Content indicates success).
- Verify the response body (if API returns the updated resource).
- Assert that changes are correctly applied by checking response fields or sending a subsequent GET request to fetch updated data.
(4) Error Handling
- If you send invalid data, the server should return 400 Bad Request.
- If you update a non-existing resource, you might get 404 Not Found.
- These scenarios should also be tested.
To test a PUT API using Playwright Java, you can use the APIRequestContext interface provided by Playwright. A PUT request is generally used to update an existing resource on the server. Here's how you can test it step-by-step:
https://reqres.in/api/users/2
Below is the input JSON request for PUT API
{ "name": "Himanshu", "job": "Software Developer" }
Java Code to Test PUT API with Playwright
import com.microsoft.playwright.*; import com.microsoft.playwright.options.*; import java.util.*; public class PutApiTest { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { APIRequest request = playwright.request(); APIRequestContext requestContext = request.newContext(new APIRequest.NewContextOptions() .setBaseURL("https://reqres.in/api")); // Prepare JSON request body Map<String, Object> data = new HashMap<>(); data.put("name", "Himanshu"); data.put("job", "Software Developer"); // Send PUT request APIResponse response = requestContext.put("/users/2", RequestOptions.create() .setHeader("Content-Type", "application/json") .setData(data) ); // Print status code System.out.println("Status Code: " + response.status()); // Print response body System.out.println("Response Body: " + response.text()); // Optional: Validate status code if (response.ok()) { System.out.println("PUT request successful."); } else { System.out.println("PUT request failed."); } } } }
Code explanation:
(a) Create object of APIRequestContext class
(b) Prepare JSON request body
(c) Called PUT request using APIRequestContext object
(d) Print the status code
(e) Print the response
(d) Validate status code
Output:
Status Code: 200 Response Body: { "name": "Himanshu", "job": "Software Developer", "updatedAt": "2025-07-28T12:13:45.012Z" } PUT request successful.
Above is the output of PUT API. We can see status code is 200 as API is successfully executed and have response in the response body.
Important Points:
APIRequestContext.put()is used to send the PUT request.- You can validate more fields using:
response.json().get("name").toString();
- Always set the
Content-Typeheader for JSON payloads.
Suggested Posts:
1. Automate GET API in Playwright
2. Automate POST API in Playwright
3. Automate DELETE API in Playwright
4. Automate Lombok API in Playwright
5. Test API by POJO Class in Playwright