RequestOptions
Added in: v1.18The 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
requestOptions.create()
Added in: v1.18Creates new instance of RequestOptions.
Returns
RequestOptions
requestOptions.setData()
Added in: v1.18Sets the request's post data.
Arguments
datastring | Buffer | SerializableAllows to set post data of the request. If the data parameter is an object, it will be serialized to json string and
content-typeheader will be set toapplication/jsonif not explicitly set. Otherwise thecontent-typeheader will be set toapplication/octet-streamif not explicitly set.
Returns
RequestOptions
requestOptions.setFailOnStatusCode()
Added in: v1.18Arguments
Whether to throw on response codes other than 2xx and 3xx. By default response object is returned for all status codes.
Returns
RequestOptions
requestOptions.setForm()
Added in: v1.18Provides 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.
Arguments
Form data to be serialized as html form using
application/x-www-form-urlencodedencoding and sent as this request body.
Returns
RequestOptions
requestOptions.setHeader()
Added in: v1.18Sets an HTTP header to the request. This header will apply to the fetched request as well as any redirects initiated by it.
Arguments
Returns
RequestOptions
requestOptions.setIgnoreHTTPSErrors()
Added in: v1.18Arguments
Returns
RequestOptions
requestOptions.setMaxRedirects()
Added in: v1.26Arguments
Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is exceeded. Defaults to
20. Pass0to not follow redirects.
Returns
RequestOptions
requestOptions.setMaxRetries()
Added in: v1.46Arguments
Maximum number of times network errors should be retried. Currently only
ECONNRESETerror is retried. Does not retry based on HTTP response codes. An error will be thrown if the limit is exceeded. Defaults to0- no retries.
Returns
RequestOptions
requestOptions.setMethod()
Added in: v1.18Changes the request method (e.g. PUT or POST).
Arguments
Request method, e.g. POST.
Returns
RequestOptions
requestOptions.setMultipart()
Added in: v1.18Provides 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.
Arguments
Form data to be serialized as html form using
multipart/form-dataencoding and sent as this request body.
Returns
RequestOptions
requestOptions.setQueryParam()
Added in: v1.18Adds a query parameter to the request URL.
Arguments
Returns
RequestOptions
requestOptions.setTimeout()
Added in: v1.18Sets request timeout in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout.
Arguments
Returns
RequestOptions