Reputation: 95
I know.... this is not the best practice, but i've been asked for this:
I'm trying to get an endpoint that receives a json
body with 3 parameters (int, int, int)
and has a Bearer Authentication.
Headers
header.setContentType(MediaType.APPLICATION_JSON);
header.set("authorization", bearerToken);
Entity
HttpEntity<String> entity = new HttpEntity<>(requestjson.toString(), header);
RestTemplate
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
System.out.print(entity):
<{"1stParam":1,"2ndParam":4881,"3rdParam":0},{Content-Type=[application/json], authorization=[Bearer bearerToken]}>
besides security and all that kind of problems, the request shows a 400 HTTP
response
via RestTemplate
, Postman works fine.
I also tried HttpURLConnection
but had same problem.
Thank you for your help :)
Upvotes: 2
Views: 6559
Reputation: 23119
I just coded a similar solution, so I'm pretty sure I know what this is. You have to include Bearer
in the Authorization
header. I think this code change will fix your problem:
header.set("Authorization", "Bearer " + bearerToken);
I capitalized the header key. I don't think this matters, but I know that the capitalized version just worked for me. My code looks just like this, and it works.
The basic idea here is that an Authorization
header has to always start with the kind of authorization being performed, so that the server can know kind you want to use. It makes sense that this is a 400 error and not a 404, because the header is technically malformed.
Upvotes: 1