1

I am trying to call this API via postman:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void printDetails(final MultivaluedMap<String, String> formParams) {

    for(String key : formParams.keySet()) {
        System.out.println(key + "     " + formParams.get(key));
    }
}

But the map turns out to be empty. Please help me with the same.

PS: This is the first time I am trying to pass variable number of parameters to the api. I have referred to sending List/Map as POST parameter jersey and How to access parameters in a RESTful POST method.

I think my mistake is in the way I am passing the parameters in postman: postman image

Please help me with the same. Also please help with how to call this API via an ajax (in JS) call.

Billal Begueradj
  • 13,551
  • 37
  • 84
  • 109
Nannan AV
  • 377
  • 3
  • 18
  • I am wondering whether the answer was helpful? Please let me know if it doesn't and what you get after trying this solution, so that I can look at it and resolve. – notionquest Oct 10 '16 at 11:56
  • @notionquest Sorry for the late reply. I just tried your answer. Unfortunately it also gave me an empty map! – Nannan AV Dec 20 '16 at 11:25
  • Are you still facing the same issue or managed to find a solution? – notionquest Dec 20 '16 at 11:27
  • @notionquest i have found one possible solution. Refer to my answer below. Thank you for trying :) – Nannan AV Dec 20 '16 at 12:01

2 Answers2

2

Set the request header as "application/x-www-form-urlencoded".

Postman request header

Request body - Select raw and provide values as mentioned below:-

{
    "LOCATION": "Singapore"
}

enter image description here

notionquest
  • 29,710
  • 3
  • 87
  • 92
1

I have found out one possible answer.

@POST
public void printDetails() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
    Map<String, String[]> mapp =  request.getParameterMap();
    for(String key : mapp.keySet()) {
        System.out.println(key + "     " + mapp.get(key)[0]);
    }
}

Still not sure how to do it by passing "final MultivaluedMap" in the arguments

Nannan AV
  • 377
  • 3
  • 18