Class APIRequestContextImpl

All Implemented Interfaces:
APIRequestContext

class APIRequestContextImpl extends ChannelOwner implements APIRequestContext
  • Field Details

  • Constructor Details

    • APIRequestContextImpl

      APIRequestContextImpl(ChannelOwner parent, String type, String guid, com.google.gson.JsonObject initializer)
  • Method Details

    • delete

      public APIResponse delete(String url, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) DELETE request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
      Specified by:
      delete in interface APIRequestContext
      Parameters:
      url - Target URL.
      options - Optional request parameters.
    • dispose

      public void dispose()
      Description copied from interface: APIRequestContext
      All responses returned by APIRequestContext.get() and similar methods are stored in the memory, so that you can later call APIResponse.body(). This method discards all stored responses, and makes APIResponse.body() throw "Response disposed" error.
      Specified by:
      dispose in interface APIRequestContext
    • fetch

      public APIResponse fetch(String urlOrRequest, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. JSON objects can be passed directly to the request.

      **Usage**

      
       Map<String, Object> data = new HashMap();
       data.put("title", "Book Title");
       data.put("body", "John Doe");
       request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));
       

      The common way to send file(s) in the body of a request is to encode it as form fields with multipart/form-data encoding. You can achieve that with Playwright API like this:

      
       // Pass file path to the form data constructor:
       Path file = Paths.get("team.csv");
       APIResponse response = request.fetch("https://example.com/api/uploadTeamList",
         RequestOptions.create().setMethod("post").setMultipart(
           FormData.create().set("fileField", file)));
      
       // Or you can pass the file content directly as FilePayload object:
       FilePayload filePayload = new FilePayload("f.js", "text/javascript",
             "console.log(2022);".getBytes(StandardCharsets.UTF_8));
       APIResponse response = request.fetch("https://example.com/api/uploadTeamList",
         RequestOptions.create().setMethod("post").setMultipart(
           FormData.create().set("fileField", filePayload)));
       
      Specified by:
      fetch in interface APIRequestContext
      Parameters:
      urlOrRequest - Target URL or Request to get all parameters from.
      options - Optional request parameters.
    • fetch

      public APIResponse fetch(Request request, RequestOptions optionsArg)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects. JSON objects can be passed directly to the request.

      **Usage**

      
       Map<String, Object> data = new HashMap();
       data.put("title", "Book Title");
       data.put("body", "John Doe");
       request.fetch("https://example.com/api/createBook", RequestOptions.create().setMethod("post").setData(data));
       

      The common way to send file(s) in the body of a request is to encode it as form fields with multipart/form-data encoding. You can achieve that with Playwright API like this:

      
       // Pass file path to the form data constructor:
       Path file = Paths.get("team.csv");
       APIResponse response = request.fetch("https://example.com/api/uploadTeamList",
         RequestOptions.create().setMethod("post").setMultipart(
           FormData.create().set("fileField", file)));
      
       // Or you can pass the file content directly as FilePayload object:
       FilePayload filePayload = new FilePayload("f.js", "text/javascript",
             "console.log(2022);".getBytes(StandardCharsets.UTF_8));
       APIResponse response = request.fetch("https://example.com/api/uploadTeamList",
         RequestOptions.create().setMethod("post").setMultipart(
           FormData.create().set("fileField", filePayload)));
       
      Specified by:
      fetch in interface APIRequestContext
      Parameters:
      request - Target URL or Request to get all parameters from.
      optionsArg - Optional request parameters.
    • fetchImpl

      private APIResponse fetchImpl(String url, RequestOptionsImpl options)
    • isJsonContentType

      private static boolean isJsonContentType(Map<String,String> headers)
    • serializeMultipartData

      private static com.google.gson.JsonArray serializeMultipartData(Map<String,Object> data)
    • get

      public APIResponse get(String url, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) GET request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

      **Usage**

      Request parameters can be configured with params option, they will be serialized into the URL search parameters:

      
       request.get("https://example.com/api/getText", RequestOptions.create()
         .setQueryParam("isbn", "1234")
         .setQueryParam("page", 23));
       
      Specified by:
      get in interface APIRequestContext
      Parameters:
      url - Target URL.
      options - Optional request parameters.
    • head

      public APIResponse head(String url, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) HEAD request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
      Specified by:
      head in interface APIRequestContext
      Parameters:
      url - Target URL.
      options - Optional request parameters.
    • patch

      public APIResponse patch(String url, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) PATCH request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
      Specified by:
      patch in interface APIRequestContext
      Parameters:
      url - Target URL.
      options - Optional request parameters.
    • post

      public APIResponse post(String url, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) POST request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.

      **Usage**

      JSON objects can be passed directly to the request:

      
       Map<String, Object> data = new HashMap();
       data.put("title", "Book Title");
       data.put("body", "John Doe");
       request.post("https://example.com/api/createBook", RequestOptions.create().setData(data));
       

      To send form data to the server use form option. Its value will be encoded into the request body with application/x-www-form-urlencoded encoding (see below how to use multipart/form-data form encoding to send files):

      
       request.post("https://example.com/api/findBook", RequestOptions.create().setForm(
           FormData.create().set("title", "Book Title").set("body", "John Doe")
       ));
       

      The common way to send file(s) in the body of a request is to upload them as form fields with multipart/form-data encoding. You can achieve that with Playwright API like this:

      
       // Pass file path to the form data constructor:
       Path file = Paths.get("team.csv");
       APIResponse response = request.post("https://example.com/api/uploadTeamList",
         RequestOptions.create().setMultipart(
           FormData.create().set("fileField", file)));
      
       // Or you can pass the file content directly as FilePayload object:
       FilePayload filePayload = new FilePayload("f.js", "text/javascript",
             "console.log(2022);".getBytes(StandardCharsets.UTF_8));
       APIResponse response = request.post("https://example.com/api/uploadTeamList",
         RequestOptions.create().setMultipart(
           FormData.create().set("fileField", filePayload)));
       
      Specified by:
      post in interface APIRequestContext
      Parameters:
      url - Target URL.
      options - Optional request parameters.
    • put

      public APIResponse put(String url, RequestOptions options)
      Description copied from interface: APIRequestContext
      Sends HTTP(S) PUT request and returns its response. The method will populate request cookies from the context and update context cookies from the response. The method will automatically follow redirects.
      Specified by:
      put in interface APIRequestContext
      Parameters:
      url - Target URL.
      options - Optional request parameters.
    • storageState

      public String storageState(APIRequestContext.StorageStateOptions options)
      Description copied from interface: APIRequestContext
      Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.
      Specified by:
      storageState in interface APIRequestContext
    • ensureOptions

      private static RequestOptionsImpl ensureOptions(RequestOptions options, String method)