跳到主要內容

RequestOptions

RequestOptions 允許建立表單資料,以便透過 APIRequestContext 發送。 Playwright 將自動判斷請求的內容類型。

context.request().post(
"https://example.com/submit",
RequestOptions.create()
.setQueryParam("page", 1)
.setData("My data"));

上傳 html 表單資料

FormData 類別可用於將表單發送到伺服器,預設情況下,請求將使用 application/x-www-form-urlencoded 編碼

context.request().post("https://example.com/signup", RequestOptions.create().setForm(
FormData.create()
.set("firstName", "John")
.set("lastName", "Doe")));

您也可以將檔案作為 html 表單的欄位發送。資料將使用 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)));

或者,您可以手動建立檔案酬載

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)));

方法

create

新增於:v1.18 requestOptions.create

建立 RequestOptions 的新實例。

用法

RequestOptions.create();

回傳


setData

新增於:v1.18 requestOptions.setData

設定請求的 post 資料。

用法

RequestOptions.setData(data);

引數

  • data String | byte[] | Object#

    允許設定請求的 post 資料。如果 data 參數是物件,它將被序列化為 json 字串,並且如果未明確設定,content-type 標頭將設定為 application/json。否則,如果未明確設定,content-type 標頭將設定為 application/octet-stream

回傳


setFailOnStatusCode

新增於:v1.18 requestOptions.setFailOnStatusCode

用法

RequestOptions.setFailOnStatusCode(failOnStatusCode);

引數

  • failOnStatusCode boolean#

    是否在 2xx 和 3xx 以外的回應代碼上拋出錯誤。預設情況下,會為所有狀態碼回傳回應物件。

回傳


setForm

新增於:v1.18 requestOptions.setForm

提供 FormData 物件,該物件將使用 application/x-www-form-urlencoded 編碼序列化為 html 表單,並作為此請求的主體發送。如果指定此參數,除非明確提供,否則 content-type 標頭將設定為 application/x-www-form-urlencoded

用法

RequestOptions.setForm(form);

引數

  • form FormData#

    要使用 application/x-www-form-urlencoded 編碼序列化為 html 表單並作為此請求主體發送的表單資料。

回傳


setHeader

新增於:v1.18 requestOptions.setHeader

為請求設定 HTTP 標頭。此標頭將適用於提取的請求以及由此請求啟動的任何重新導向。

用法

RequestOptions.setHeader(name, value);

引數

回傳


setIgnoreHTTPSErrors

新增於:v1.18 requestOptions.setIgnoreHTTPSErrors

用法

RequestOptions.setIgnoreHTTPSErrors(ignoreHTTPSErrors);

引數

  • ignoreHTTPSErrors boolean#

    發送網路請求時是否忽略 HTTPS 錯誤。

回傳


setMaxRedirects

新增於:v1.26 requestOptions.setMaxRedirects

用法

RequestOptions.setMaxRedirects(maxRedirects);

引數

  • maxRedirects int#

    將自動追蹤的最大請求重新導向次數。如果超過此數字,將拋出錯誤。預設值為 20。傳遞 0 以不追蹤重新導向。

回傳


setMaxRetries

新增於:v1.46 requestOptions.setMaxRetries

用法

RequestOptions.setMaxRetries(maxRetries);

引數

  • maxRetries int#

    網路錯誤應重試的最大次數。目前僅重試 ECONNRESET 錯誤。不會根據 HTTP 回應代碼進行重試。如果超過限制,將拋出錯誤。預設值為 0 - 不重試。

回傳


setMethod

新增於:v1.18 requestOptions.setMethod

變更請求方法(例如 PUTPOST)。

用法

RequestOptions.setMethod(method);

引數

回傳


setMultipart

新增於:v1.18 requestOptions.setMultipart

提供 FormData 物件,該物件將使用 multipart/form-data 編碼序列化為 html 表單,並作為此請求的主體發送。如果指定此參數,除非明確提供,否則 content-type 標頭將設定為 multipart/form-data

用法

RequestOptions.setMultipart(form);

引數

  • form FormData#

    要使用 multipart/form-data 編碼序列化為 html 表單並作為此請求主體發送的表單資料。

回傳


setQueryParam

新增於:v1.18 requestOptions.setQueryParam

將查詢參數新增至請求 URL。

用法

RequestOptions.setQueryParam(name, value);

引數

回傳


setTimeout

新增於:v1.18 requestOptions.setTimeout

設定請求逾時時間,以毫秒為單位。預設值為 30000(30 秒)。傳遞 0 以停用逾時。

用法

RequestOptions.setTimeout(timeout);

引數

  • timeout double#

    請求逾時時間,以毫秒為單位。

回傳