Spring RestTemplate – exchange() method with GET and POST Requests

 

The exchange() method

Execute the HTTP method to the given URI template, writing the given HttpEntity to the request, and returns the response as ResponseEntity.

In below, i am going to show you some sample RestClient exchange requests with GET and POST HTTP methods.

 

 

GET request with No Request Parameters (With Headers)

In here the HTTP GET request is made without any query params (request params) and Basic Authentication header. Therefore by observing the below example, you can get an idea of how exchange method is used to send HTTP GET request without request parameters and headers.

 

 

GET request with Request Parameters (Query Params) and Headers

In here, the HTTP GET request is made with query parameters (request parameters) and Basic Authentication header. Therefore by observing the below example, you can get an idea of how exchange method is used to send HTTP GET request with request params and headers.

The generated request URL will be something like below. You can see that it includes the query params.

http://localhost:53793/users/1?name=chathuranga&email=chathuranga.t@gmail.com

 

 

 

POST request with Request Body and Headers

In here the HTTP POST request is made with valid request body and Basic Authentication header. Therefore by observing the below example, you can get an idea of how exchange method is used to send HTTP POST request with request body and headers.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s