Class APIRequestContextImpl
- All Implemented Interfaces:
APIRequestContext
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.microsoft.playwright.APIRequestContext
APIRequestContext.StorageStateOptions -
Field Summary
FieldsFields inherited from class com.microsoft.playwright.impl.ChannelOwner
connection, guid, initializer, type -
Constructor Summary
ConstructorsConstructorDescriptionAPIRequestContextImpl(ChannelOwner parent, String type, String guid, com.google.gson.JsonObject initializer) -
Method Summary
Modifier and TypeMethodDescriptiondelete(String url, RequestOptions options) Sends HTTP(S) DELETE request and returns its response.voiddispose()All responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body().private static RequestOptionsImplensureOptions(RequestOptions options, String method) fetch(Request request, RequestOptions optionsArg) Sends HTTP(S) request and returns its response.fetch(String urlOrRequest, RequestOptions options) Sends HTTP(S) request and returns its response.private APIResponsefetchImpl(String url, RequestOptionsImpl options) get(String url, RequestOptions options) Sends HTTP(S) GET request and returns its response.head(String url, RequestOptions options) Sends HTTP(S) HEAD request and returns its response.private static booleanisJsonContentType(Map<String, String> headers) patch(String url, RequestOptions options) Sends HTTP(S) PATCH request and returns its response.post(String url, RequestOptions options) Sends HTTP(S) POST request and returns its response.put(String url, RequestOptions options) Sends HTTP(S) PUT request and returns its response.private static com.google.gson.JsonArrayserializeMultipartData(Map<String, Object> data) Returns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.Methods inherited from class com.microsoft.playwright.impl.ChannelOwner
adopt, disposeChannelOwner, handleEvent, runUntil, sendMessage, sendMessage, sendMessageAsync, toProtocolRef, withLogging, withWaitLoggingMethods inherited from class com.microsoft.playwright.impl.LoggingSupport
logApi, logApiIfEnabled, logWithTimestamp, withLoggingMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.microsoft.playwright.APIRequestContext
delete, fetch, fetch, get, head, patch, post, put, storageState
-
Field Details
-
tracing
-
-
Constructor Details
-
APIRequestContextImpl
APIRequestContextImpl(ChannelOwner parent, String type, String guid, com.google.gson.JsonObject initializer)
-
-
Method Details
-
delete
Description copied from interface:APIRequestContextSends 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:
deletein interfaceAPIRequestContext- Parameters:
url- Target URL.options- Optional request parameters.
-
dispose
public void dispose()Description copied from interface:APIRequestContextAll responses returned byAPIRequestContext.get()and similar methods are stored in the memory, so that you can later callAPIResponse.body(). This method discards all stored responses, and makesAPIResponse.body()throw "Response disposed" error.- Specified by:
disposein interfaceAPIRequestContext
-
fetch
Description copied from interface:APIRequestContextSends 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-dataencoding. 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:
fetchin interfaceAPIRequestContext- Parameters:
urlOrRequest- Target URL or Request to get all parameters from.options- Optional request parameters.
-
fetch
Description copied from interface:APIRequestContextSends 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-dataencoding. 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:
fetchin interfaceAPIRequestContext- Parameters:
request- Target URL or Request to get all parameters from.optionsArg- Optional request parameters.
-
fetchImpl
-
isJsonContentType
-
serializeMultipartData
-
get
Description copied from interface:APIRequestContextSends 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
paramsoption, 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:
getin interfaceAPIRequestContext- Parameters:
url- Target URL.options- Optional request parameters.
-
head
Description copied from interface:APIRequestContextSends 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:
headin interfaceAPIRequestContext- Parameters:
url- Target URL.options- Optional request parameters.
-
patch
Description copied from interface:APIRequestContextSends 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:
patchin interfaceAPIRequestContext- Parameters:
url- Target URL.options- Optional request parameters.
-
post
Description copied from interface:APIRequestContextSends 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
formoption. Its value will be encoded into the request body withapplication/x-www-form-urlencodedencoding (see below how to usemultipart/form-dataform 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-dataencoding. 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:
postin interfaceAPIRequestContext- Parameters:
url- Target URL.options- Optional request parameters.
-
put
Description copied from interface:APIRequestContextSends 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:
putin interfaceAPIRequestContext- Parameters:
url- Target URL.options- Optional request parameters.
-
storageState
Description copied from interface:APIRequestContextReturns storage state for this request context, contains current cookies and local storage snapshot if it was passed to the constructor.- Specified by:
storageStatein interfaceAPIRequestContext
-
ensureOptions
-