4

I'm trying to write a rest endpoint which receives application/x-www-form-urlencoded. But the endpoint does not accept request parameters for @RequestBody or @RequestParam

I Have tried using MultiValueMap to grab the request parameters. But I always get 0 parameters. Is there a way to get request values to the MultiValueMap or some other POJO class.

AD=&value=sometestvalue - This is the application/x-www-form-urlencoded requestbody. I'm trying to do the request using postman

@RequestMapping(value = "/test/verification/pay/{id}", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseBody
public Response testVerificationPay(@PathVariable("id") long id, @RequestParam MultiValueMap formData,
                                           HttpServletRequest servletRequest, ServiceContext serviceContext){
        log.info("!--REQUEST START--!"+formData.toString()); 
} 
Shenali Silva
  • 127
  • 3
  • 9

3 Answers3

2

You need to use MultiValueMap<String, String>

@RequestMapping(value = "/test/verification/pay/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseBody
    public Response testVerificationPay(@PathVariable("id") long id, @RequestParam MultiValueMap<String, String> formData) {
        System.out.println("!--REQUEST START--!" + formData.toString());
        return null;
    }
yancey
  • 21
  • 3
0

You do not use @RequestParam on a POST request as the data is not in the URL as in a GET request.

You should use @RequestBody (doc) along with registering appropriate HttpMessageConverter. Most likely you should use: FormHttpMessageConverter

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • I have tried using @RequestBody as well still I get formData size as 0. which means no parameters – Shenali Silva Aug 05 '19 at 09:25
  • https://stackoverflow.com/questions/33796218/content-type-application-x-www-form-urlencodedcharset-utf-8-not-supported-for tried this solution. Still the same result – Shenali Silva Aug 05 '19 at 09:29
  • I know this was quite some time ago but did you ever find a solution to this? I experience the same problem now. – Niklas Eldberger Jan 12 '23 at 11:54
0

Try @ResponseBody. Then, change to a String, not a MultiValueMap, to see if the body comes in to the request.

WonChul Heo
  • 242
  • 1
  • 12
  • Tried. it gives an empty String. – Shenali Silva Aug 05 '19 at 10:30
  • 1
    I wasn't able to find the Spring documentation to handle this. would be grateful if anyone can guide me there. surely there must be a way to handle url-encoded from requests in Spring :) – Shenali Silva Aug 05 '19 at 10:31
  • then use httpservletrequest. (ex) `servletRequest.getParameter("value")`)If you still do not respond, your client is passing data in an incorrect way. – WonChul Heo Aug 05 '19 at 14:53