Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making authenticated POST requests with Spring RestTemplate for Android

I have a RESTful API I'm trying to connect with via Android and RestTemplate. All requests to the API are authenticated with HTTP Authentication, through setting the headers of the HttpEntity and then using RestTemplate's exchange() method.

All GET requests work great this way, but I cannot figure out how to accomplish authenticated POST requests. postForObject and postForEntity handle POSTs, but have no easy way to set the Authentication headers.

So for GETs, this works great:

HttpAuthentication httpAuthentication = new HttpBasicAuthentication("username", "password"); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(httpAuthentication);  HttpEntity<?> httpEntity = new HttpEntity<Object>(requestHeaders);  MyModel[] models = restTemplate.exchange("/api/url", HttpMethod.GET, httpEntity, MyModel[].class); 

But POSTs apparently don't work with exchange() as it never sends the customized headers and I don't see how to set the request body using exchange().

What is the easiest way to make authenticated POST requests from RestTemplate?

like image 662
Nick Daugherty Avatar asked Apr 27 '12 21:04

Nick Daugherty


People also ask

How do I set authentication on RestTemplate?

To enable basic authentication in RestTemplate for outgoing rest requests, we shall configure CredentialsProvider into HttpClient API. This HttpClient will be used by RestTemplate to send HTTP requests to backend rest apis. In this example, we are creating a Junit test which invokes a basic auth secured rest api.

How do I pass the authorization token in RestTemplate?

Setting bearer token for a GET request RestTemplate restTemplate = new RestTemplate(); String customerAPIUrl = "http://localhost:9080/api/customer"; HttpHeaders headers = new HttpHeaders(); headers. set("Authorization", "Bearer " + accessToken); //accessToken can be the secret key you generate. headers.

How do you call a post method using RestTemplate in spring boot?

Consuming POST API by using RestTemplate - exchange() method Assume this URL http://localhost:8080/products returns the response shown below, we are going to consume this API response by using the Rest Template. Autowired the Rest Template Object. Use the HttpHeaders to set the Request Headers.

Which method of RestTemplate is used for sending post request?

Here, we'll try to send POST requests to the Person API by using the POST methods provided by the RestTemplate: postForObject, postForEntity, and postForLocation.


2 Answers

Ok found the answer. exchange() is the best way. Oddly the HttpEntity class doesn't have a setBody() method (it has getBody()), but it is still possible to set the request body, via the constructor.

// Create the request body as a MultiValueMap MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();       body.add("field", "value");  // Note the body object as first parameter! HttpEntity<?> httpEntity = new HttpEntity<Object>(body, requestHeaders);  ResponseEntity<MyModel> response = restTemplate.exchange("/api/url", HttpMethod.POST, httpEntity, MyModel.class); 
like image 119
Nick Daugherty Avatar answered Oct 08 '22 07:10

Nick Daugherty


Slightly different approach:

MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>(); headers.add("HeaderName", "value"); headers.add("Content-Type", "application/json");  RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());  HttpEntity<ObjectToPass> request = new HttpEntity<ObjectToPass>(objectToPass, headers);  restTemplate.postForObject(url, request, ClassWhateverYourControllerReturns.class); 
like image 26
Andrew Avatar answered Oct 08 '22 07:10

Andrew