The RequestOptions allows to create form data to be sent via APIRequestContext. 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
FormData class can be used to send a form to the server, by default the request will use application/x-www-form-urlencoded
encoding:
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)));
Methods createAdded in: v1.18 requestOptions.create
Creates new instance of RequestOptions.
Usage
Returns
setDataAdded in: v1.18 requestOptions.setDataSets the request's post data.
Usage
RequestOptions.setData(data);
Arguments
data
String | byte[] | Object#
Allows to set post data of the request. If the data parameter is an object, it will be serialized to json string and content-type
header will be set to application/json
if not explicitly set. Otherwise the content-type
header will be set to application/octet-stream
if not explicitly set.
Returns
setFailOnStatusCodeAdded in: v1.18 requestOptions.setFailOnStatusCodeUsage
RequestOptions.setFailOnStatusCode(failOnStatusCode);
Arguments
Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
Returns
setFormAdded in: v1.18 requestOptions.setFormProvides FormData object that will be serialized as html form using application/x-www-form-urlencoded
encoding and sent as this request body. If this parameter is specified content-type
header will be set to application/x-www-form-urlencoded
unless explicitly provided.
Usage
RequestOptions.setForm(form);
Arguments
Form data to be serialized as html form using application/x-www-form-urlencoded
encoding and sent as this request body.
Returns
Added in: v1.18 requestOptions.setHeaderSets an HTTP header to the request. This header will apply to the fetched request as well as any redirects initiated by it.
Usage
RequestOptions.setHeader(name, value);
Arguments
Returns
setIgnoreHTTPSErrorsAdded in: v1.18 requestOptions.setIgnoreHTTPSErrorsUsage
RequestOptions.setIgnoreHTTPSErrors(ignoreHTTPSErrors);
Arguments
Returns
setMaxRedirectsAdded in: v1.26 requestOptions.setMaxRedirectsUsage
RequestOptions.setMaxRedirects(maxRedirects);
Arguments
Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to 20
. Pass 0
to not follow redirects.
Returns
setMaxRetriesAdded in: v1.46 requestOptions.setMaxRetriesUsage
RequestOptions.setMaxRetries(maxRetries);
Arguments
Maximum number of times network errors should be retried. Currently only ECONNRESET
error is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to 0
- no retries.
Returns
setMethodAdded in: v1.18 requestOptions.setMethodChanges the request method (e.g. PUT or POST).
Usage
RequestOptions.setMethod(method);
Arguments
Returns
setMultipartAdded in: v1.18 requestOptions.setMultipartProvides FormData object that will be serialized as html form using multipart/form-data
encoding and sent as this request body. If this parameter is specified content-type
header will be set to multipart/form-data
unless explicitly provided.
Usage
RequestOptions.setMultipart(form);
Arguments
Form data to be serialized as html form using multipart/form-data
encoding and sent as this request body.
Returns
setQueryParamAdded in: v1.18 requestOptions.setQueryParamAdds a query parameter to the request URL.
Usage
RequestOptions.setQueryParam(name, value);
Arguments
Returns
setTimeoutAdded in: v1.18 requestOptions.setTimeoutSets request timeout in milliseconds. Defaults to 30000
(30 seconds). Pass 0
to disable timeout.
Usage
RequestOptions.setTimeout(timeout);
Arguments
Returns
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4