如何POST形成参数?
How to POST form parameters?
我正在尝试发送 HTTP POST 表单参数。我知道 Content-Type 必须是 application/x-www-form-urlencoded
并且参数必须在请求正文中编码。
In version 9.0.6, Jetty 似乎已经自动完成了:
The POST request is sent with the application/x-www-form-urlencoded content type, and POST parameter values are automatically URL-encoded.
但是 in version 9.4.19,我看到文档已经更改,现在它说:
The POST parameter values added via the param() method are automatically URL-encoded.
果然调用了:
httpClient.POST("http://example.com/entity/1")
.param("p", "value")
.send();
现在将参数视为 URL 查询参数。没有任何内容被添加到请求正文中。
正确 POST 表单参数的正确方法是什么?
HTML 表单是 POST 请求的主体内容。
所以这意味着您必须使用 org.eclipse.jetty.client.api.ContentProvider
和 Request.content(ContentProvider)
API.
有 2 个 ContentProvider
实现用于 HTML 表单。
FormContentProvider
- class:
org.eclipse.jetty.client.util.FormContentProvider
- 内容类型:
application/x-www-form-urlencoded
import org.eclipse.jetty.client.util.FormContentProvider;
import org.eclipse.jetty.util.Fields;
Fields fields = new Fields();
fields.put("fruit", "apple");
httpClient.POST("http://example.com/entity/1")
.content(new FormContentProvider(fields))
.send();
MultiPartContentProvider
- class:
org.eclipse.jetty.client.util.MultiPartContentProvider
- 内容类型:
multipart/form-data
import org.eclipse.jetty.client.util.MultiPartContentProvider;
import org.eclipse.jetty.client.util.StringContentProvider;
MultiPartContentProvider multiPart = new MultiPartContentProvider();
multiPart.addFieldPart("fruit", new StringContentProvider("apple"), null);
multiPart.close();
httpClient.POST("http://example.com/entity/1")
.content(multiPart)
.send();
我正在尝试发送 HTTP POST 表单参数。我知道 Content-Type 必须是 application/x-www-form-urlencoded
并且参数必须在请求正文中编码。
In version 9.0.6, Jetty 似乎已经自动完成了:
The POST request is sent with the application/x-www-form-urlencoded content type, and POST parameter values are automatically URL-encoded.
但是 in version 9.4.19,我看到文档已经更改,现在它说:
The POST parameter values added via the param() method are automatically URL-encoded.
果然调用了:
httpClient.POST("http://example.com/entity/1")
.param("p", "value")
.send();
现在将参数视为 URL 查询参数。没有任何内容被添加到请求正文中。
正确 POST 表单参数的正确方法是什么?
HTML 表单是 POST 请求的主体内容。
所以这意味着您必须使用 org.eclipse.jetty.client.api.ContentProvider
和 Request.content(ContentProvider)
API.
有 2 个 ContentProvider
实现用于 HTML 表单。
FormContentProvider
- class:
org.eclipse.jetty.client.util.FormContentProvider
- 内容类型:
application/x-www-form-urlencoded
import org.eclipse.jetty.client.util.FormContentProvider;
import org.eclipse.jetty.util.Fields;
Fields fields = new Fields();
fields.put("fruit", "apple");
httpClient.POST("http://example.com/entity/1")
.content(new FormContentProvider(fields))
.send();
MultiPartContentProvider
- class:
org.eclipse.jetty.client.util.MultiPartContentProvider
- 内容类型:
multipart/form-data
import org.eclipse.jetty.client.util.MultiPartContentProvider;
import org.eclipse.jetty.client.util.StringContentProvider;
MultiPartContentProvider multiPart = new MultiPartContentProvider();
multiPart.addFieldPart("fruit", new StringContentProvider("apple"), null);
multiPart.close();
httpClient.POST("http://example.com/entity/1")
.content(multiPart)
.send();