Class APIRequestContextImpl

    • Constructor Detail

      • APIRequestContextImpl

        APIRequestContextImpl​(ChannelOwner parent,
                              java.lang.String type,
                              java.lang.String guid,
                              com.google.gson.JsonObject initializer)
    • Method Detail

      • delete

        public APIResponse delete​(java.lang.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.
      • fetch

        public APIResponse fetch​(java.lang.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.
      • isJsonContentType

        private static boolean isJsonContentType​(java.util.Map<java.lang.String,​java.lang.String> headers)
      • serializeMultipartData

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

        public APIResponse get​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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​(java.lang.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.