Setting custom header on Spring RestTemplate GET call

Sujit

I am using Spring REST Template to call an external public REST API. As part of the API authentication I need send the user-key in the header. I am not sure how to set the custom header attribute in Spring REST template GET call.

RestTemplate restTemplate = new RestTemplate();
<Class> object = restTemplate.getForObject("<url>","<class type>");

I found this can be done with HttpHeaders class by setting set("key","value") but didn't find any concrete example. Let me know if you have any info.

Sujit

To pass a custom attribute in REST request via request Header, we need to create a new HTTPHeaders object and set the key and value by set method and pass to HttpEntity as shown below.

Next RestTemplate, exchange() method can be which has a method param of HttpEntity.

 HttpHeaders headers = new HttpHeaders();
 headers.set("custom-header-key","custom-header-value");
 HttpEntity<String> entity = new HttpEntity<>("paramters",headers);

 RestTemplate restTemplate = new RestTemplate();
 ResponseEntity<ResponseObj> responseObj = restTemplate.exchange("<end point url>", HttpMethod.GET,entity,ResponseObj.class);
 ResponseObj resObj = responseObj.getBody();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting request header content-type to json in Spring Framework resttemplate

From Dev

Is there a way to pass header information in Spring RestTemplate DELETE call

From Dev

How to get Response Header information from Spring RestTemplate GET request

From Dev

Resttemplate GET Request with Custom Headers

From Java

How to set an "Accept:" header on Spring RestTemplate request?

From Dev

Set default content type header of Spring RestTemplate

From Dev

Setting custom header using HttpURLConnection

From Dev

Dropwizard - Setting a custom vary header

From Dev

Setting custom header using HttpURLConnection

From Dev

Dropwizard - Setting a custom vary header

From Dev

How to call MultipartFile Spring REST URL with RestTemplate

From Dev

Unable to call Salesforce API using Spring RestTemplate

From Dev

How to use Spring Cache Redis with a custom RestTemplate?

From Dev

Get custom request header

From Dev

RestTemplate exchange fail on GET call but works on CURL

From Dev

Converting GET response from restTemplate to Custom Class

From Dev

How to send POST request through RestTemplate with custom parameter in header

From Dev

Setting custom header NAME with jQuery Ajax

From Dev

The $_SERVER variable is not setting my custom header

From Dev

Setting a header 400 with custom description -- is this possible?

From Java

Get list of JSON objects with Spring RestTemplate

From Dev

Get size of serialized Request using Spring RestTemplate

From Dev

Spring RestTemplate - Passing in object parameters in GET

From Dev

How to get List from Object in Spring RestTemplate

From Dev

Spring restTemplate get raw json string

From Dev

Setting CORS call to GET not OPTIONS

From Dev

Spring Integration not forwarding a custom header?

From Dev

Get request failed with custom header

From Dev

Get custom header using javascript?

Related Related

  1. 1

    Setting request header content-type to json in Spring Framework resttemplate

  2. 2

    Is there a way to pass header information in Spring RestTemplate DELETE call

  3. 3

    How to get Response Header information from Spring RestTemplate GET request

  4. 4

    Resttemplate GET Request with Custom Headers

  5. 5

    How to set an "Accept:" header on Spring RestTemplate request?

  6. 6

    Set default content type header of Spring RestTemplate

  7. 7

    Setting custom header using HttpURLConnection

  8. 8

    Dropwizard - Setting a custom vary header

  9. 9

    Setting custom header using HttpURLConnection

  10. 10

    Dropwizard - Setting a custom vary header

  11. 11

    How to call MultipartFile Spring REST URL with RestTemplate

  12. 12

    Unable to call Salesforce API using Spring RestTemplate

  13. 13

    How to use Spring Cache Redis with a custom RestTemplate?

  14. 14

    Get custom request header

  15. 15

    RestTemplate exchange fail on GET call but works on CURL

  16. 16

    Converting GET response from restTemplate to Custom Class

  17. 17

    How to send POST request through RestTemplate with custom parameter in header

  18. 18

    Setting custom header NAME with jQuery Ajax

  19. 19

    The $_SERVER variable is not setting my custom header

  20. 20

    Setting a header 400 with custom description -- is this possible?

  21. 21

    Get list of JSON objects with Spring RestTemplate

  22. 22

    Get size of serialized Request using Spring RestTemplate

  23. 23

    Spring RestTemplate - Passing in object parameters in GET

  24. 24

    How to get List from Object in Spring RestTemplate

  25. 25

    Spring restTemplate get raw json string

  26. 26

    Setting CORS call to GET not OPTIONS

  27. 27

    Spring Integration not forwarding a custom header?

  28. 28

    Get request failed with custom header

  29. 29

    Get custom header using javascript?

HotTag

Archive