java.lang.Object
net.serenitybdd.screenplay.playwright.interactions.api.APIRequest
All Implemented Interfaces:
net.serenitybdd.core.SkipNested, net.serenitybdd.screenplay.Performable

public class APIRequest extends Object implements net.serenitybdd.screenplay.Performable
Make API requests within the browser session context. This allows API testing with the same cookies and authentication as the browser, enabling hybrid UI+API test scenarios.

The API requests share the browser's session, so if you've logged in via the UI, API calls will automatically include authentication cookies.

Sample usage:

     // Simple GET request
     actor.attemptsTo(APIRequest.get("/api/users"));

     // POST with JSON body
     actor.attemptsTo(
         APIRequest.post("/api/users")
             .withJsonBody(Map.of("name", "John", "email", "john@example.com"))
     );

     // Request with headers
     actor.attemptsTo(
         APIRequest.get("/api/protected")
             .withHeader("X-Custom-Header", "value")
     );

     // Query the response
     int status = actor.asksFor(LastAPIResponse.statusCode());
     String body = actor.asksFor(LastAPIResponse.body());
     Map<String, Object> json = actor.asksFor(LastAPIResponse.jsonBody());
 
See Also:
  • Method Details

    • get

      public static APIRequest get(String url)
      Create a GET request.
      Parameters:
      url - The URL or path to request
    • post

      public static APIRequest post(String url)
      Create a POST request.
      Parameters:
      url - The URL or path to request
    • put

      public static APIRequest put(String url)
      Create a PUT request.
      Parameters:
      url - The URL or path to request
    • patch

      public static APIRequest patch(String url)
      Create a PATCH request.
      Parameters:
      url - The URL or path to request
    • delete

      public static APIRequest delete(String url)
      Create a DELETE request.
      Parameters:
      url - The URL or path to request
    • head

      public static APIRequest head(String url)
      Create a HEAD request.
      Parameters:
      url - The URL or path to request
    • to

      public static APIRequest to(String method, String url)
      Create a request with a custom HTTP method.
      Parameters:
      method - The HTTP method
      url - The URL or path to request
    • withHeader

      public APIRequest withHeader(String name, String value)
      Add a header to the request.
      Parameters:
      name - Header name
      value - Header value
    • withHeaders

      public APIRequest withHeaders(Map<String,String> headers)
      Add multiple headers to the request.
      Parameters:
      headers - Map of header names to values
    • withQueryParam

      public APIRequest withQueryParam(String name, String value)
      Add a query parameter to the request URL.
      Parameters:
      name - Parameter name
      value - Parameter value
    • withQueryParams

      public APIRequest withQueryParams(Map<String,String> params)
      Add multiple query parameters to the request URL.
      Parameters:
      params - Map of parameter names to values
    • withJsonBody

      public APIRequest withJsonBody(Object jsonBody)
      Set the request body as a JSON object. Automatically sets Content-Type to application/json.
      Parameters:
      jsonBody - Object to serialize as JSON (Map, List, or POJO)
    • withBody

      public APIRequest withBody(String body)
      Set the request body as a string.
      Parameters:
      body - The request body
    • withBody

      public APIRequest withBody(byte[] body)
      Set the request body as bytes.
      Parameters:
      body - The request body
    • withContentType

      public APIRequest withContentType(String contentType)
      Set the Content-Type header.
      Parameters:
      contentType - The content type (e.g., "application/xml")
    • withTimeout

      public APIRequest withTimeout(int timeoutMs)
      Set the request timeout in milliseconds.
      Parameters:
      timeoutMs - Timeout in milliseconds
    • failOnStatusCode

      public APIRequest failOnStatusCode(boolean fail)
      Configure whether to throw an exception on non-2xx status codes. Default is false (does not throw).
      Parameters:
      fail - If true, throws on non-2xx responses
    • performAs

      public <T extends net.serenitybdd.screenplay.Actor> void performAs(T actor)
      Specified by:
      performAs in interface net.serenitybdd.screenplay.Performable
    • getLastApiResponseKey

      public static String getLastApiResponseKey()
      Get the key used to store the last API response in actor's memory.