# RestTemplate GET Request with Parameters and Headers

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Nov 9, 2019, updated Sep 24, 2022. Topics: Spring Boot.
Canonical URL: https://attacomsian.com/blog/spring-boot-resttemplate-get-request-parameters-headers

> Learn how to make different kinds of HTTP GET requests with query parameters, custom request headers, basic HTTP authentication, and more using RestTemplate. 

In this article, you will learn how to make different HTTP GET  requests using the [RestTemplate](https://attacomsian.com/blog/http-requests-resttemplate-spring-boot) class in a Spring Boot application.

## Simple GET Request

To make a GET HTTP request, you can use either `getForObject()` or `getForEntity()` method. Here is an example that uses the `getForObject()` method to fetch the user information as a [JSON](https://attacomsian.com/blog/what-is-json) string:

```java
// request url
String url = "https://jsonplaceholder.typicode.com/posts/1";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// make an HTTP GET request
String json = restTemplate.getForObject(url, String.class);

// print json
System.out.println(json);
```

## GET Request with Request Parameters

To pass query parameters, you can append them directly to the URL or use placeholders. Here is an example of a GET request made with **query parameters appended** to the URL:

```java
// request url
String url = "https://google.com/search?q=java";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// make an HTTP GET request
String html = restTemplate.getForObject(url, String.class);
```

Similarly, you can add **placeholders** to the URL for query parameters:

```java
// request url
String url = "https://google.com/search?q={q}";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// make an HTTP GET request
String html = restTemplate.getForObject(url, String.class, "java");
```

## GET Request with Parameters and Headers

To add custom request headers to an HTTP GET request, you should use the generic `exchange()` method provided by the `RestTemplate` class.

The following GET request is made with **query parameters and request headers**:

```java
// request url
String url = "https://jsonplaceholder.typicode.com/posts/{id}";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// create headers
HttpHeaders headers = new HttpHeaders();

// set `Content-Type` and `Accept` headers
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

// example of custom header
headers.set("X-Request-Source", "Desktop");

// build the request
HttpEntity request = new HttpEntity(headers);

// make an HTTP GET request with headers
ResponseEntity<String> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        String.class,
        1
);

// check response
if (response.getStatusCode() == HttpStatus.OK) {
    System.out.println("Request Successful.");
    System.out.println(response.getBody());
} else {
    System.out.println("Request Failed");
    System.out.println(response.getStatusCode());
}
```

## GET Request with Basic Authentication

The following example demonstrates how to add [basic authentication to RestTemplate](https://attacomsian.com/blog/resttemplate-basic-authentication) GET request:

```java
// request url
String url = "https://jsonplaceholder.typicode.com/posts";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// create headers
HttpHeaders headers = new HttpHeaders();

// add basic authentication header
headers.setBasicAuth("username", "password");

// build the request
HttpEntity request = new HttpEntity(headers);

// make an HTTP GET request with headers
ResponseEntity<String> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        String.class
);

// check response
if (response.getStatusCode() == HttpStatus.OK) {
    System.out.println("Request Successful.");
    System.out.println(response.getBody());
} else {
    System.out.println("Request Failed");
    System.out.println(response.getStatusCode());
}
```

## GET Request with Response Mapped to Java Object

Using RestTemplate, you can also **map the JSON response directly to a Java object**. Let us first create a simple model class:

<p class="file-desc"><span>Post.java</span></p>

```java
public class Post implements Serializable {

    private int userId;
    private int id;
    private String title;
    private String body;

    public Post() {
    }

    public Post(int userId, int id, String title, String body) {
        this.userId = userId;
        this.id = id;
        this.title = title;
        this.body = body;
    }

    // getters and setters, equals(), toString() .... (omitted for brevity)
}
```

Now we can simply use the `Post` class as a response type in the `exchange()` method:

```java
// request url
String url = "https://jsonplaceholder.typicode.com/posts/1";

// create an instance of RestTemplate
RestTemplate restTemplate = new RestTemplate();

// create headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

// build the request
HttpEntity request = new HttpEntity(headers);

// make an HTTP GET request with headers
ResponseEntity<Post> response = restTemplate.exchange(
        url,
        HttpMethod.GET,
        request,
        Post.class
);

// check response
if (response.getStatusCode() == HttpStatus.OK) {
    System.out.println("Request Successful.");
    System.out.println(response.getBody());
} else {
    System.out.println("Request Failed");
    System.out.println(response.getStatusCode());
}
```

Check out the [Making HTTP Requests using RestTemplate in Spring Boot](https://attacomsian.com/blog/http-requests-resttemplate-spring-boot) guide for more RestTemplate examples.
