Caner Aydın
Caner Aydın

Reputation: 315

Content type 'application/x-www-form-urlencoded' not supported" for post method

@RestController
@RequestMapping("/user")
public class UserController {

    @PostMapping(value = "/",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<InternalUser> saveInternalUser(@RequestBody InternalUserDTO dto){

       InternalUser user =  internalUserService.saveInternalUser(dto);
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }

this is my class. WHen i use postman with valid json to that url http://localhost:8090/user

i get error of

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported]

i tried this https://stackoverflow.com/a/38252762/11369236

I dont want to use requestparam like here

https://stackoverflow.com/a/43065261/11369236

Some answers suggest to use

          consumes = MediaType.APPLICATION_JSON,

but spring gives errors:

attribute must be constant

What can i do?

Upvotes: 0

Views: 25888

Answers (2)

Avinash Gupta
Avinash Gupta

Reputation: 228

Use body > raw to send your JSON data for the API request

{
    "name": "John Doe"
    "email": "[email protected]"
}

POSTMAN-POST-REQUEST

Upvotes: 2

Bohdan Petrenko
Bohdan Petrenko

Reputation: 1175

You have to add Content-type: application/json;charset=utf-8 header to your request.

By default (if Content-type header is absent) content type of POST request body is application/x-www-form-urlencoded. In @RequestMapping by consumes = MediaType.APPLICATION_JSON_UTF8_VALUE mentioned that json in UTF-8 encoding expected in request body. Thats why spring throws an exception.

Upvotes: 5

Related Questions