Put-Request throws 401 [no body] error and cannot be stored in the response entity

Viewed 11434

I want to change data on a server via a put request, but I always get a 401 [no body] error. The response looks like the following:

enter image description here

I do not really understand why I get this error, because my body is not empty. My code looks like this and the values seem to be okay too. Does anyone have any idea what I'm doing wrong?

enter image description here enter image description here

Postman Update:

The values are different right now (consent and authorisation) since its basically a new request but the values were correct before too so this change should not make a difference.

enter image description here

enter image description here

4 Answers

Looks like you are simply passing invalid authorization header, or maybe not passing it at all.

What happens is that you make a RestTemplate exchange call, then you get 401 from that request, and Spring propagates it and returns 500 - Internal Server Error, because there is no error handling in place.

EDIT: According to your screenshots, you are not replacing your path variables. Update the way you build your URL as listed below.

Map<String, String> pathVars = new HashMap<>(2);
pathVars.put("consent-id", consentId);
pathVars.put("authorisation-id", authorisationId);

UriComponents uri = UriComponentsBuilder.fromUri(mainLink)
    .path("consents/{consent-id}/authorizations/{authorisation-id}")
    .buildAndExpand(pathVars);

Verify if your authorization-id is correct

if the token has a type for example Bearer you must write so:

"Authorization": "Bearer rrdedzfdgf........."

and make sure that there is only one space between Bearer and the token

Often the problem comes from the browser locally; if your site is online, save the part and deploy the last modifications of the site and make the test

otherwise if it is a mobile application test it on a smartphone and not a browser;

in case none of this works, do it with your backend, it works with this

I had a problem where the I would add an extra character to a password. And Insomnia(Or Postman) would return a JSON response from the server along with a 401 HTTP status code. But when I did the same thing inside a springboot app, when using catch(HttpServerErrorException e){System.out.prinln(e.getMessage());} the e.getMessage would have [no body]. I think that is a feature built in the HttpServerErrorException class where it doesn't provide the body for security purposes. Since whoever is requesting is not authorized they should not have access to it.

Related