Package com.microsoft.playwright.options
Interface RequestOptions
-
public interface RequestOptionsTheRequestOptionsallows to create form data to be sent viaAPIRequestContext. Playwright will automatically determine content type of the request.context.request().post( "https://example.com/submit", RequestOptions.create() .setQueryParam("page", 1) .setData("My data"));Uploading html form data
FormDataclass can be used to send a form to the server, by default the request will useapplication/x-www-form-urlencodedencoding:context.request().post("https://example.com/signup", RequestOptions.create().setForm( FormData.create() .set("firstName", "John") .set("lastName", "Doe")));You can also send files as fields of an html form. The data will be encoded using
multipart/form-data:Path path = Paths.get("members.csv"); APIResponse response = context.request().post("https://example.com/upload_members", RequestOptions.create().setMultipart(FormData.create().set("membersList", path)));Alternatively, you can build the file payload manually:
FilePayload filePayload = new FilePayload("members.csv", "text/csv", "Alice, 33\nJohn, 35\n".getBytes(StandardCharsets.UTF_8)); APIResponse response = context.request().post("https://example.com/upload_members", RequestOptions.create().setMultipart(FormData.create().set("membersList", filePayload)));
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static RequestOptionscreate()Creates new instance ofRequestOptions.RequestOptionssetData(byte[] data)Sets the request's post data.RequestOptionssetData(Object data)Sets the request's post data.RequestOptionssetData(String data)Sets the request's post data.RequestOptionssetFailOnStatusCode(boolean failOnStatusCode)RequestOptionssetForm(FormData form)ProvidesFormDataobject that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body.RequestOptionssetHeader(String name, String value)Sets an HTTP header to the request.RequestOptionssetIgnoreHTTPSErrors(boolean ignoreHTTPSErrors)RequestOptionssetMaxRedirects(int maxRedirects)RequestOptionssetMethod(String method)Changes the request method (e.g.RequestOptionssetMultipart(FormData form)ProvidesFormDataobject that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body.RequestOptionssetQueryParam(String name, boolean value)Adds a query parameter to the request URL.RequestOptionssetQueryParam(String name, int value)Adds a query parameter to the request URL.RequestOptionssetQueryParam(String name, String value)Adds a query parameter to the request URL.RequestOptionssetTimeout(double timeout)Sets request timeout in milliseconds.
-
-
-
Method Detail
-
create
static RequestOptions create()
Creates new instance ofRequestOptions.- Since:
- v1.18
-
setData
RequestOptions setData(String data)
Sets the request's post data.- Parameters:
data- Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.- Since:
- v1.18
-
setData
RequestOptions setData(byte[] data)
Sets the request's post data.- Parameters:
data- Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.- Since:
- v1.18
-
setData
RequestOptions setData(Object data)
Sets the request's post data.- Parameters:
data- Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string andcontent-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.- Since:
- v1.18
-
setFailOnStatusCode
RequestOptions setFailOnStatusCode(boolean failOnStatusCode)
- Parameters:
failOnStatusCode- Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.- Since:
- v1.18
-
setForm
RequestOptions setForm(FormData form)
ProvidesFormDataobject that will be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set toapplication/x-www-form-urlencodedunless explicitly provided.- Parameters:
form- Form data to be serialized as html form usingapplication/x-www-form-urlencodedencoding and sent as this request body.- Since:
- v1.18
-
setHeader
RequestOptions setHeader(String name, String value)
Sets an HTTP header to the request. This header will apply to the fetched request as well as any redirects initiated by it.- Parameters:
name- Header name.value- Header value.- Since:
- v1.18
-
setIgnoreHTTPSErrors
RequestOptions setIgnoreHTTPSErrors(boolean ignoreHTTPSErrors)
- Parameters:
ignoreHTTPSErrors- Whether to ignore HTTPS errors when sending network requests.- Since:
- v1.18
-
setMaxRedirects
RequestOptions setMaxRedirects(int maxRedirects)
- Parameters:
maxRedirects- Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to20. Pass0to not follow redirects.- Since:
- v1.26
-
setMethod
RequestOptions setMethod(String method)
- Parameters:
method- Request method, e.g. POST.- Since:
- v1.18
-
setMultipart
RequestOptions setMultipart(FormData form)
ProvidesFormDataobject that will be serialized as html form usingmultipart/form-dataencoding and sent as this request body. If this parameter is specifiedcontent-typeheader will be set tomultipart/form-dataunless explicitly provided.- Parameters:
form- Form data to be serialized as html form usingmultipart/form-dataencoding and sent as this request body.- Since:
- v1.18
-
setQueryParam
RequestOptions setQueryParam(String name, String value)
Adds a query parameter to the request URL.- Parameters:
name- Parameter name.value- Parameter value.- Since:
- v1.18
-
setQueryParam
RequestOptions setQueryParam(String name, boolean value)
Adds a query parameter to the request URL.- Parameters:
name- Parameter name.value- Parameter value.- Since:
- v1.18
-
setQueryParam
RequestOptions setQueryParam(String name, int value)
Adds a query parameter to the request URL.- Parameters:
name- Parameter name.value- Parameter value.- Since:
- v1.18
-
setTimeout
RequestOptions setTimeout(double timeout)
Sets request timeout in milliseconds. Defaults to30000(30 seconds). Pass0to disable timeout.- Parameters:
timeout- Request timeout in milliseconds.- Since:
- v1.18
-
-