mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-15 06:02:57 +03:00
docs(java): more FormData examples (#15156)
This commit is contained in:
parent
d7b63fa0b4
commit
7ebd81c1fa
@ -1,7 +1,8 @@
|
|||||||
# class: RequestOptions
|
# class: RequestOptions
|
||||||
* langs: java
|
* langs: java
|
||||||
|
|
||||||
The [RequestOptions] allows to create form data to be sent via [APIRequestContext].
|
The [RequestOptions] allows to create form data to be sent via [APIRequestContext]. Playwright will automatically
|
||||||
|
determine content type of the request.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
context.request().post(
|
context.request().post(
|
||||||
@ -11,6 +12,33 @@ context.request().post(
|
|||||||
.setData("My data"));
|
.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:
|
||||||
|
|
||||||
|
```java
|
||||||
|
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`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST):
|
||||||
|
|
||||||
|
```java
|
||||||
|
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:
|
||||||
|
```java
|
||||||
|
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: RequestOptions.create
|
## method: RequestOptions.create
|
||||||
- returns: <[RequestOptions]>
|
- returns: <[RequestOptions]>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user