1

Is it possible to get POST parameters from REST request?

I tried the following with no success:

MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
log.info("Params Size: "+params.size());
Iterator<String> it = params.keySet().iterator();
String theKey = null;
while(it.hasNext()){
    theKey = it.next();
    log.info("Here is a Key: "+theKey);
}

Here is my method signature:

@POST
@Produces("application/pdf")
@Path("/hello")
public Response producePDF(@FormParam("filename")String fileName, @Context UriInfo uriInfo)

Logs show 0 for "Params Size:"

Can I only use a GET?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • @SotiriosDelimanolis A Jersey injectable which provides information about the URI that was invoked. – Eric Stein Sep 03 '14 at 15:52
  • And what does its `getPathParameters()` method do? – Sotirios Delimanolis Sep 03 '14 at 15:52
  • possible duplicate of [How can I grab all query parameters in Jersey JaxRS?](http://stackoverflow.com/questions/5718575/how-can-i-grab-all-query-parameters-in-jersey-jaxrs) – Eric Stein Sep 03 '14 at 15:53
  • You say "parameters", but I don't think that's what you actually mean. – chrylis -cautiouslyoptimistic- Sep 03 '14 at 15:55
  • @SotiriosDelimanolis It returns all the variables on the path. So if the path is specified in Jersey as `/resource/{resourceId}`, and the client invoked `http://server/resource/123`, that method would return one item, 123. – Eric Stein Sep 03 '14 at 15:55
  • 1
    You first have to understand that parameters in POST requests **are not** passed through query string like GET requests. POST supports them but in a decent app/REST API you will not pass POST parameters through query string but in the body of the request. – Luiggi Mendoza Sep 03 '14 at 15:58
  • @EricStein :) I want OP to answer my questions so they can see where they went wrong. – Sotirios Delimanolis Sep 03 '14 at 16:00
  • I guess @MichaelClayton is refering to parameters send via a web formular f.e. like `@Post @Consumes("application/x-www-form-urlencoded") @Path("/somePath") public MultilvaluedMap handlePostRequest(MultivaluedMap form) { ... }` – Roman Vottner Sep 03 '14 at 16:01
  • I understand that, that is why my question is about POST, not GET : ) –  Sep 03 '14 at 16:09
  • Here is my method signature: public Response producePDF(@FormParam("filename")String fileName, @Context UriInfo uriInfo) –  Sep 03 '14 at 16:12
  • 1
    @MichaelClayton have you tried to inject the `MultivaluedMap` directly instead of the `UriInfo` object - this should be valid in JAX-RS 2 containers (like Jersey). Furthermore, if you try to process parameters sent from a web formular, please edit your question to include that information - and also provide your method-signature (including any annotation you've specified) – Roman Vottner Sep 03 '14 at 16:13
  • 1
    @MichaelClayton See [**this**](http://books.google.com.mx/books?id=_jQtCL5_vAcC&pg=PA76&lpg=PA76&dq=MultivaluedMap&source=bl&ots=cL7vdiSZ3Y&sig=rP-UHbROHnG4HU0_ROzfRRhkAQY&hl=en&sa=X&ei=Pj4HVMWuPI-mggSJ24HoDQ&ved=0CEUQ6AEwBQ#v=onepage&q=MultivaluedMap&f=false) in Google Books. – Paul Vargas Sep 03 '14 at 16:16
  • Btw. I can highly recommend that book if you are dealing with JAX-RS ;) (although SO is not the platform for recommendations) – Roman Vottner Sep 03 '14 at 16:17
  • @PaulVargas post an answer. There's no duplicate of this question in SO. – Luiggi Mendoza Sep 03 '14 at 16:17
  • @LuiggiMendoza Link-only answers are no answers, so it is appropriate as comment – Roman Vottner Sep 03 '14 at 16:18
  • @RomanVottner who said something to post a link only answer? I tell PaulVargas to provide a decent answer using the info on that link. He's a 11k user, so I don't need to specify all this stuff (AFAIK). – Luiggi Mendoza Sep 03 '14 at 16:19
  • @Eric Stein I used that SO question, and this is the same code. no success. –  Sep 03 '14 at 16:28

4 Answers4

2

@Roman Vottner Your answer was it. I needed to inject the MultivaluedMap instead of constructing at method call.

Code:

@POST
    @Produces("application/pdf")
    @Path("/hello")
    @Consumes("application/x-www-form-urlencoded")
    public Response producePDF(MultivaluedMap<String, String> params)

Iterator<String> it = params.keySet().iterator();
            String theKey = null;
            while(it.hasNext()){
                theKey = it.next();
                log.info("Here is a Key: "+theKey);
                if(theKey.equals("filename")){
                    fileName = params.getFirst(theKey);
                    System.out.println("Key: "+theKey);
                }
            }

I can now get the parameters!

0

If by "POST parameter" you mean "query parameter", then you want uriInfo.getQueryParameters(). If not, you'll need to explain what you actually mean.

Eric Stein
  • 13,209
  • 3
  • 37
  • 52
  • I have a html form, method="POST". I switched to getQueryParameters, but no good. I am guessing getQueryParameters are for GET? I don't know what or how many parameters will come in the request, so I want to iterate them. –  Sep 03 '14 at 16:10
0

If you are using a HTML form, you can try the next:

HTML:

<form action="rest/resource/hello" method="post" target="_blank">
    <fieldset>
        <legend>Download PDF</legend>
        <label>Filename: <input type="text" name="filename" required></label>
        <button>Submit</button>
    </fieldset>
</form>

Java:

@POST
@Path("/hello")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("application/pdf")
public Response producePDF(@FormParam("filename") String fileName) {

    // Do something

    ...
}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

For Json

@POST
@Consumes("application/json")
public void fRestEndPoint(Map<String, String> params) throws IOException, JSONException {
    log.info("Received a f rest call ");
    String output = params.toString();
    log.info(output);
rinjan
  • 550
  • 5
  • 19