Unsupported Media Type in postman

I am implementing spring security with oauth2 and jwt. the below is my login function

function doLogin(loginData) {


$.ajax({
url :  back+"/auth/secret",
type : "POST",
data : JSON.stringify(loginData),
contentType : "application/json; charset=utf-8",
dataType : "json",
async : false,
success : function(data, textStatus, jqXHR) {


setJwtToken(data.token);




},
error : function(jqXHR, textStatus, errorThrown) {
alert("an unexpected error occured: " + errorThrown);
window.location.href= back+'/login_page.html';
}
});
}

And down I have the Controller

 @RequestMapping(value = "auth/secret", method = RequestMethod.POST)
public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtAuthenticationRequest authenticationRequest, Device device) throws AuthenticationException {
System.out.println();
logger.info("authentication request : " + authenticationRequest.getUsername() + " " + authenticationRequest.getPassword());
// Perform the security
System.out.println( authenticationRequest.getUsername()+"is the username and "+authenticationRequest.getPassword());
final Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
authenticationRequest.getUsername(),
authenticationRequest.getPassword()




)




);
SecurityContextHolder.getContext().setAuthentication(authentication);


logger.info("authentication passed");


// Reload password post-security so we can generate token
final UserDetails userDetails = userDetailsService.loadUserByUsername(authenticationRequest.getUsername());
final String token = jwtTokenUtil.generateToken(userDetails, device);
logger.info("token " + token);


// Return the token
return ResponseEntity.ok(new JwtAuthenticationResponse(token));
}

But when I try the post request with the postman it shows me

{
"timestamp": 1488973010828,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundaryY4KgeeQ9ONtKpvkQ;charset=UTF-8' not supported",
"path": "/TaxiVis/auth/secret"
}

But when I do cosole.log(data) in the ajax call it prints the token?I could not figure out what is wrong.Any help is appreciated.

260957 次浏览

You need to set the content-type in postman as JSON (application/json).

Go to the body inside your POST request, there you will find the raw option.

Right next to it, there will be a drop down, select JSON (application.json).

Http 415 Media Unsupported is responded back only when the content type header you are providing is not supported by the application.

With POSTMAN, the Content-type header you are sending is Content type 'multipart/form-data not application/json. While in the ajax code you are setting it correctly to application/json. Pass the correct Content-type header in POSTMAN and it will work.

I also got this error .I was using Text inside body after changing to XML(text/xml) , got result as expected.

  • If your request is XML Request use XML(text/xml).

  • If your request is JSON Request use JSON(application/json)

When this was happening with me in XML;
I just changed "application/XML" to be "text/XML",
which solved my problem.

If you are still failing with Unsupported Media Type in postman when calling a SOAP endpoint you could try:

Content-Type: application/soap+xml

I had this problem. I had authentication on the authentication tab set up to pass credentials in body.

This error occurred for me when I had the Body set to None.

So I needed an empty body in postman, set to raw JSON to allow this to work even though my main request was parameters in the querystring.

{
}

i was also having a similar issue. in my case i made two changes

Click on headers tag and add a key 'Content-Type' with Value 'application/json'

enter image description here

Second step is to click on Body tab and select 'raw' radio button and select type as 'JSON' from dropdown as shown below

enter image description here