96

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.

3
  • 3
    "message": "Content type 'multipart/form-data; You are not sending a json payload
    – alfcope
    Commented Mar 8, 2017 at 11:47
  • What does that mean? Could you please explain?
    – user7477092
    Commented Mar 8, 2017 at 11:49
  • 1
    That means your request is wrong because the mapping method at your controller, using @RequestBody, is waiting for a json object. You need to set the header Content-Type as application-json in postman and put the json object you want to send at the body section as raw. google.com/search?q=postman%20json%20post&&rct=j
    – alfcope
    Commented Mar 8, 2017 at 12:04

8 Answers 8

269

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

4
  • It is in JSON only
    – user7477092
    Commented Mar 8, 2017 at 12:23
  • 1
    in ur request mapping u can specifically add @consumes with values as application/json to work with JSON object Commented Mar 9, 2017 at 7:17
  • 4
    Just to be clear, the dropdown only appears to the right once you've selected the "raw" option.
    – adam0101
    Commented Feb 17, 2019 at 16:33
  • 3
    Sometimes in the request-BODY, you have to put (at the least) some empty brackets. Aka {} Commented Sep 25, 2019 at 19:14
18

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

1
  • This was the right answer for me - seems like a bug in Postman that this is necessary.
    – Adam Wise
    Commented Aug 7, 2023 at 15:01
13

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.

6

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)

5

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

Content-Type: application/soap+xml

3

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.

{
}
2

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

0

In PostMan is was facing issue so i solved it by selecting the format for raw data from text to JSON then after it started working! enter image description here