Using jersey to read form data

I am developing a web application where I have this form

<form name="form" action="create-user" method="post"> <input name="accept" type="checkbox"><span>{{acceptLegalTerms}}</span><br> <input type="submit" value="{{Continue}}" class="primary fright"/> </form> 

On the server side, we use Jersey (on GAE). And here is what I am trying to use to read POST values

 @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Path("create-user") public Response createUser(@FormDataParam("accept") boolean acceptForm) { return Response.ok().entity(acceptForm).build(); } 

But that doesn't work ... He brings me back ...

 HTTP ERROR 415 Problem accessing /login/create-user. Reason: Unsupported Media Type 

Any ideas? What am I doing wrong?

Thanks!

+6
source share
2 answers

try the following:

 @Path("test") @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String testForm(@FormParam("accept") String accept) { return accept; } 

Multipart is something a little different, see the jersey sample multipart-webapp example or see http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html . Your web form does not produce it, so Jersey correctly returns 415 - Type of unsupported media, because you do not have any resource that handles the media type "application / x-www-form-urlencoded".

+13
source

Just to make it simple: if it is the only request handler that maps to a specific URL (in this case, "test") and a specific HTTP method (POST), you can avoid using @Consumes!

+1
source

Source: https://habr.com/ru/post/908874/


All Articles