Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap

300,745

Solution 1

The problem is that when we use application/x-www-form-urlencoded, Spring doesn't understand it as a RequestBody. So, if we want to use this we must remove the @RequestBody annotation.

Then try the following:

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
        produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody  Representation authenticate(@PathVariable("email") String anEmailAddress, MultiValueMap paramMap) throws Exception {
   if(paramMap == null && paramMap.get("password") == null) {
        throw new IllegalArgumentException("Password not provided");
    }
    return null;
}

Note that removed the annotation @RequestBody

answer: Http Post request with content type application/x-www-form-urlencoded not working in Spring

Solution 2

It seems that now you can just mark the method parameter with @RequestParam and it will do the job for you.

@PostMapping( "some/request/path" )
public void someControllerMethod( @RequestParam Map<String, String> body ) {
  //work with Map
}

Solution 3

Add a header to your request to set content type to application/json

curl -H 'Content-Type: application/json' -s -XPOST http://your.domain.com/ -d YOUR_JSON_BODY

this way spring knows how to parse the content.

Solution 4

In Spring 5

@PostMapping( "some/request/path" )
public void someControllerMethod( @RequestParam MultiValueMap body ) {

    // import org.springframework.util.MultiValueMap;

    String datax = (String) body .getFirst("datax");
}

Solution 5

@RequestBody MultiValueMap paramMap

in here Remove the @RequestBody Annotaion

@RequestMapping(value = "/signin",method = RequestMethod.POST)
public String createAccount(@RequestBody LogingData user){
    logingService.save(user);
    return "login";
}




@RequestMapping(value = "/signin",method = RequestMethod.POST)
public String createAccount( LogingData user){
    logingService.save(user);
    return "login";
} 

like that

Share:
300,745
Somasundaram Sekar
Author by

Somasundaram Sekar

Updated on November 29, 2021

Comments

  • Somasundaram Sekar
    Somasundaram Sekar over 2 years

    Based on the answer for problem with x-www-form-urlencoded with Spring @Controller

    I have written the below @Controller method

    @RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
                , produces = {"application/json", "application/xml"}
                ,  consumes = {"application/x-www-form-urlencoded"}
        )
         public
            @ResponseBody
            Representation authenticate(@PathVariable("email") String anEmailAddress,
                                        @RequestBody MultiValueMap paramMap)
                    throws Exception {
    
    
                if(paramMap == null || paramMap.get("password") == null) {
                    throw new IllegalArgumentException("Password not provided");
                }
        }
    

    the request to which fails with the below error

    {
      "timestamp": 1447911866786,
      "status": 415,
      "error": "Unsupported Media Type",
      "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
      "message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
      "path": "/users/usermail%40gmail.com/authenticate"
    }
    

    [PS: Jersey was far more friendly, but couldn't use it now given the practical restrictions here]

  • kholofelo Maloma
    kholofelo Maloma about 7 years
    Thank you! Solves the problem. Now I wonder how do we explicitly remove the application/x-www-form-urlencoded ?
  • razvanone
    razvanone almost 7 years
    You might need to also add an Accept header to your command: 'curl -vk -H "Accept: application/json" -H "Content-Type: application/json" ' etc.
  • Osama Al-Banna
    Osama Al-Banna over 6 years
    can you please explain how to add this setting to my HTML form ?
  • Douglas Ribeiro
    Douglas Ribeiro about 6 years
    it is not necessary @kholofeloMaloma
  • NemanjaT
    NemanjaT over 4 years
    Yeah, with inclusion of consumer=MediaType.APPLICATION_FORM_URLENCODED_VALUE in mapping, you deserve more points sir! thank you! @RequestParam seams to be required now for picking up MultiValueMap from the request
  • bluemind
    bluemind about 4 years
    If anyone wondered why this works without any annotation, it seems Spring handles any non annotated arguments as if they have @ModelAttribute, even though this behaviour is (sadly) not documented. And @ModelAttribute does understand x-www-form-urlencoded
  • withoutOne
    withoutOne almost 4 years
    public ResponseEntity<?> getToken(MultiValueMap paramMap) IllegalArgumentException: argument type mismatch
  • torez233
    torez233 over 3 years
    Thanks for the info! As a newbie, I am wondering what's the reason behind this little odd behaviour for Spring to parse the payload and bind it to object?
  • Yunnosch
    Yunnosch over 3 years
    While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
  • mending3
    mending3 about 3 years
    this is valid on Spring Boot 2.4.2
  • Hermann Steidel
    Hermann Steidel over 2 years
    This is the simplest answer; works for me on Spring Boot 2.6.0 on a @RestController.
  • MarkAddison
    MarkAddison almost 2 years
    I believe that should be OR as opposed to AND (if paraMap is null the get will produce NPE ` if(paramMap == null || paramMap.get("password") == null) { throw new IllegalArgumentException("Password not provided"); } `