28

I am developing RESTful services with Jersey and it works great with GET methods. But I can only get null parameters using the POST method. Here is the sample code from my project.

HTML

<form action="rest/console/sendemail" method="post">
  <input type="text" id="email" name="email">
  <button type="submit">Send</button>
</form> 

Java

@POST
@Path("/sendemail")
public Response sendEmail(@QueryParam("email") String email) {
    System.out.println(email);
    return  new Response();
}

The email I get from the post is always null. Anyone has the idea?

I changed QueryParam to FormParam, the parameter I get is still null.

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
user1573269
  • 363
  • 1
  • 4
  • 9

2 Answers2

39

In a form submitted via POST, email is not a @QueryParam like in /sendemail?email=me@example.com.

If you submit your HTML form via POST, email is a @FormParam.

Edit:

This is a minimal JAX-RS Resource that can deal with your HTML form.

package rest;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/console")
public class Console {

    @POST
    @Path("/sendemail")
    @Produces(MediaType.TEXT_PLAIN)
    public Response sendEmail(@FormParam("email") String email) {
        System.out.println(email);
        return Response.ok("email=" + email).build();
    }
}
  • 3
    +1 though it is strictly only true for form encoding; if the submitted document was coming as XML, you'd want to just deserialize that to be an object (of some class) _without_ any annotation that parameter. – Donal Fellows Oct 22 '12 at 09:02
  • @DonalFellows True. But in the scope of this question, we can safely assume that `@FormParam` works. –  Oct 22 '12 at 09:03
  • @Tichodroma, I changed QueryParam to FormParam, the parameter I get is still null. – user1573269 Oct 22 '12 at 09:32
  • @Tichodroma Thanks for your great help. But I use you code above, the email is still null. Any other issue leads to this? – user1573269 Oct 22 '12 at 10:54
  • @Tichodroma Yes, I checked the path, they are all fine. And the response page shows "email=null", it means the path is right, but I can not get the parameter sent by post method. If I changed it to get method, it works fine. – user1573269 Oct 22 '12 at 11:00
  • Well, the code in my answer works on GlassFish with Jersey. I have no further idea. –  Oct 22 '12 at 11:01
  • OK, I just use jetty server. I have no idea about it. Thanks for your help. – user1573269 Oct 22 '12 at 11:05
  • 1
    You have missed the @Consumes("application/x-www-form-urlencoded") annotation. – sat Mar 06 '14 at 21:51
  • sat has the answer to this question I believe. Should put it in an answer instead of the above comment. One additional recommendation is to use the constant javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED instead of the hard coded string literal. Thank you. – Jesse Ivy Feb 03 '17 at 22:36
1

Just to note one small detail - every input value you want to submit as part of your form has to have "name" attribute.

<input type="text" id="email" name="email" />
morgan_il
  • 1,501
  • 16
  • 19