Nannan AV
Nannan AV

Reputation: 419

unable to pass map to restful method

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.

Upvotes: 1

Views: 1814

Answers (2)

Nannan AV
Nannan AV

Reputation: 419

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

Upvotes: 1

notionquest
notionquest

Reputation: 39186

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

Upvotes: 2

Related Questions