Unsupported Media Type in postman

SpringSpring SecurityJwtSpring Security-Oauth2

Spring Problem Overview


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.

Spring Solutions


Solution 1 - Spring

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).

Solution 2 - Spring

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.

Solution 3 - Spring

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)

Solution 4 - Spring

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

Content-Type: application/soap+xml

Solution 5 - Spring

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

Solution 6 - Spring

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.

{
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser7477092View Question on Stackoverflow
Solution 1 - SpringAmit Kumar LalView Answer on Stackoverflow
Solution 2 - Springritesh.gargView Answer on Stackoverflow
Solution 3 - SpringMahesh ChaudharyView Answer on Stackoverflow
Solution 4 - SpringZsolt NormannView Answer on Stackoverflow
Solution 5 - SpringYasser Sayed MohammedView Answer on Stackoverflow
Solution 6 - SpringPhilip JohnsonView Answer on Stackoverflow