Class APIRequestContextImpl
- java.lang.Object
-
- com.microsoft.playwright.impl.LoggingSupport
-
- com.microsoft.playwright.impl.ChannelOwner
-
- com.microsoft.playwright.impl.APIRequestContextImpl
-
- All Implemented Interfaces:
APIRequestContext
class APIRequestContextImpl extends ChannelOwner implements APIRequestContext
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.microsoft.playwright.APIRequestContext
APIRequestContext.StorageStateOptions
-
-
Field Summary
Fields Modifier and Type Field Description private TracingImpltracing-
Fields inherited from class com.microsoft.playwright.impl.ChannelOwner
connection, guid, initializer, type
-
-
Constructor Summary
Constructors Constructor Description APIRequestContextImpl(ChannelOwner parent, java.lang.String type, java.lang.String guid, com.google.gson.JsonObject initializer)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description APIResponsedelete(java.lang.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, java.lang.String method)APIResponsefetch(Request request, RequestOptions optionsArg)Sends HTTP(S) request and returns its response.APIResponsefetch(java.lang.String urlOrRequest, RequestOptions options)Sends HTTP(S) request and returns its response.private APIResponsefetchImpl(java.lang.String url, RequestOptionsImpl options)APIResponseget(java.lang.String url, RequestOptions options)Sends HTTP(S) GET request and returns its response.APIResponsehead(java.lang.String url, RequestOptions options)Sends HTTP(S) HEAD request and returns its response.private static booleanisJsonContentType(java.util.Map<java.lang.String,java.lang.String> headers)APIResponsepatch(java.lang.String url, RequestOptions options)Sends HTTP(S) PATCH request and returns its response.APIResponsepost(java.lang.String url, RequestOptions options)Sends HTTP(S) POST request and returns its response.APIResponseput(java.lang.String url, RequestOptions options)Sends HTTP(S) PUT request and returns its response.private static com.google.gson.JsonArrayserializeMultipartData(java.util.Map<java.lang.String,java.lang.Object> data)java.lang.StringstorageState(APIRequestContext.StorageStateOptions options)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, withWaitLogging
-
Methods inherited from class com.microsoft.playwright.impl.LoggingSupport
logApi, logApiIfEnabled, logWithTimestamp, withLogging
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.microsoft.playwright.APIRequestContext
delete, fetch, fetch, get, head, patch, post, put, storageState
-
-
-
-
Field Detail
-
tracing
private final TracingImpl tracing
-
-
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: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
public APIResponse fetch(java.lang.String urlOrRequest, RequestOptions options)
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
public APIResponse fetch(Request request, RequestOptions optionsArg)
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
private APIResponse fetchImpl(java.lang.String url, RequestOptionsImpl options)
-
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: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
public APIResponse head(java.lang.String url, RequestOptions options)
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
public APIResponse patch(java.lang.String url, RequestOptions options)
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
public APIResponse post(java.lang.String url, RequestOptions options)
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
public APIResponse put(java.lang.String url, RequestOptions options)
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
public java.lang.String storageState(APIRequestContext.StorageStateOptions options)
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
private static RequestOptionsImpl ensureOptions(RequestOptions options, java.lang.String method)
-
-