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

Based on the answer to the x-www-form-urlencoded issue with Spring @Controller

I wrote below @Controller method below

@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");
            }
    }

      

a request that fails with the error below

{
  "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 much friendlier, but couldn't use it now given the practical limitations here]

+65


source to share


8 answers


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

Then try this:

@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;
}

      



Please note that the @RequestBody annotation has been removed

answer: Http post request with content type / x -www-form-urlencoded app doesn't work in Spring

+76


source


It seems you can now 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
}

      

+54


source


Add a header to your request to set the content type for the app / json

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

      

this is how spring can parse content.

+13


source


I wrote an alternative to fooobar.com/questions/12477717 / ... .

There I wrote step by step explaining with code. Short way:

First : write an object

Second : create a converter to display a model extending AbstractHttpMessageConverter

Third : ask spring to use this converter implementing WebMvcConfigurer.class overriding configureMessageConverters method

Fourth and last, when you use this parameter, implementations in the display inside your controller will consume = MediaType.APPLICATION_FORM_URLENCODED_VALUE and @RequestBody in front of your object.

I am using spring boot 2.

+1


source


Spring 5

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

    // import org.springframework.util.MultiValueMap;

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

      

+1


source


Simply removing the annotation @RequestBody

solves the problem (tested on Spring Boot 2):

@RestController
public class MyController {

    @PostMapping
    public void method(@Valid RequestDto dto) {
       // method body ...
    }
}

      

0


source


Instead of using a map, you can use the parameters directly:

   @RequestMapping(method = RequestMethod.POST, value = "/event/register")
   @ResponseStatus(value = HttpStatus.OK)
   public void registerUser(@RequestParam(name = EVENT_ID) String eventId,
                            @RequestParam(name = ATTENDEE_ID) String attendeeId,
                            @RequestParam(name = SCENARIO) String scenario) {
    log.info("Register user: eventid: {}, attendeeid: {}, scenario: {} ", eventId,attendeeId,scenario);

    //more code here
}

      

-1


source


My solution from Alamofire

toswift 3

Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON 

      

-2


source







All Articles