Using Jersey to read form data

25,277

try this:

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

Multipart is something slightly different, see jersey sample multipart-webapp or see http://www.w3.org/Protocols/rfc1341/7_2_Multipart.html. Your web form is not producing it, so Jersey correctly returns 415 - Unsupported media type, because you don't have any resource which is handling "application/x-www-form-urlencoded" media type.

Share:
25,277
Javier Manzano
Author by

Javier Manzano

JavaScript, Node.js, React, Blockchain, ...

Updated on February 21, 2020

Comments

  • Javier Manzano
    Javier Manzano about 4 years

    I'm developing a web app where i have a form like that

    <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're using Jersey (on GAE). And here's what I'm trying to use to read the 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 it doesn't work... It returns me...

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

    Any ideas? What Am I doing wrong?

    Thanks!

  • Drew Stephens
    Drew Stephens over 11 years
    You'll need jersey-multipart also.
  • Keven Wang
    Keven Wang over 10 years
    @DrewStephens Is it possible to have a single path that serves both urlencode or multipart POST requests? stackoverflow.com/questions/19104413/…
  • Vuk Stanković
    Vuk Stanković over 9 years
    Is there a way to submit form with 10 fields, but without adding 10 @FormParam lines
  • Pavel Bucek
    Pavel Bucek over 9 years