application/x-www-form-urlencoded
. Another option is multipart/form-data
.GET
and POST
requests with form data. The problems of this approach when using non- latin-1
will be discussed in this article.@Form
, @FormParam
. For a POST
query, @FormParam
equivalent to @QueryParam
for GET
behavior. With a small exception: behavior when using utf-8. In the case of using the GET
method, decoding to a string from urlencoded
occurs without problems. For POST
, the Content-Type
must also be set to charset=utf-8
, so that when decoding from urlencoded
conversion of the byte stream takes place in UTF-8, and not Latin-1 (the default behavior).Content-Type: application/x-www-form-urlencoded; charset=utf-8
header Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Type: application/x-www-form-urlencoded; charset=utf-8
. For a form without js, I could not succeed by specifying an enctype for the form.@HeaderParam("Content-Type")
to the required methods: @Path("/") public interface Rest { @POST @Path("test") public String test(@FormParam("q") String query, @HeaderParam("Content-Type") String contentType); }
Rest client = ProxyFactory.create(Rest.class, url); client.test(query, "application/x-www-form-urlencode; charset=utf-8");
charset
field to the Content-Type: application/x-www-form-urlencoded
header. It is implemented as follows: @ClientInterceptor @HeaderDecoratorPrecedence public class RestInterceptor implements ClientExecutionInterceptor { public static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded"; public static final String FORM_CONTENT_TYPE_WITH_CHARSET = "application/x-www-form-urlencoded; charset=utf-8"; @Override public ClientResponse execute(ClientExecutionContext context) throws Exception { String contentType = context.getRequest().getHeaders().getFirst(HttpHeaders.CONTENT_TYPE); if (formWithoutCharset(contentType)) { context.getRequest().header(HttpHeaders.CONTENT_TYPE, FORM_CONTENT_TYPE_WITH_CHARSET); } return context.proceed(); } private boolean formWithoutCharset(String contentType) { return contentType != null && contentType.contains(FORM_CONTENT_TYPE) && ! contentType.contains("charset"); } }
latin-1
... But this is the first step to simplifying the client code. public static void initResteasy() { ResteasyProviderFactory factory = ResteasyProviderFactory.getInstance(); RegisterBuiltin.register(factory); InterceptorRegistry<ClientExecutionInterceptor> registry = factory.getClientExecutionInterceptorRegistry(); registry.register(new RestInterceptor()); }
ClientRequest
, and using proxy objects in the process of sending a request, if there is a Content-Type
header with application/x-www-form-urlencoded
, but without a charset
, then the header containing it is put down.Source: https://habr.com/ru/post/140270/
All Articles