0

I'm trying to execute the URL by making POST http call using rest Template. However, not able to send the message body in the POST request.

Here is the code:

public static void main(String[] args) {
                
        RestTemplate restTemplate = new RestTemplate();
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<>("Hello World 3", headers);
        headers.setBearerAuth("token");
        headers.add("Header", "header1");
        
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("www.message.com/messages");

        HttpEntity<String> response = restTemplate.exchange(
                builder.toUriString(), 
                HttpMethod.GET,
                entity, 
                String.class);
    }

actually, I'm sending the body using HttpEntity but when Im getting the response, the response is not proper because if the body would have been sent properly, we would have received all the metadata in the response.

Can someone please suggest on whether it is the right way to set the body in POST request.

UPDATE 1

I used below code , still getting error about invalid payload

public static void main(String[] args) {

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth("token");
        headers.add("Header", "header1");
        String body = "hello world 4";
        HttpEntity<String> entity = new HttpEntity<String>(body, headers);
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("www.example.com");
        HttpEntity<String> response = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.POST,
                entity,
                String.class);
        System.out.println(response.getBody());

}

getting below error :

Exception in thread "main" org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: "{"error":{"code":"BadRequest","message":"Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.","innerError":{"date":"2023-06-21T16:17:39","request-id":"d26d4986-35e0-443c-801f-babf1b741757","client-request-id":"d26d4986-35e0-443c-801f-babf1b741757"}}}" at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:103) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:646) at oe.kubeapi.cloudcontroller.TeamMes4.main(TeamMes4.java:24)

daniel
  • 2,665
  • 1
  • 8
  • 18
user2315104
  • 2,378
  • 7
  • 35
  • 54
  • Does this answer your question? [POST request via RestTemplate in JSON](https://stackoverflow.com/questions/4075991/post-request-via-resttemplate-in-json) – M. Rizzo Jun 21 '23 at 13:14
  • @M.Rizzo : Im still getting error of invalid payload . please see update 1 . Thanks – user2315104 Jun 21 '23 at 16:20
  • 1
    I think your problem may be that the request structure expected by the backend service is an actual json string not a plain string. Can you give more details on what the back end is expecting for a request? You might need to create a JSONObject and then output the string. – M. Rizzo Jun 22 '23 at 12:57

2 Answers2

0

had same issue. In my case problem was @RequestBody. It seems that it don't need @RequestBody when your are accepting simple String as body as you did in your example.

String body = "Hello World 3";

   @GetMapping
    public String getData(String value) throws Exception {
        return "test response";
    }
Abhishek
  • 156
  • 1
  • 10
0

You are using GET instead of POST, Use HttpMethod.POST instead of HttpMethod.GET. And as per your payload, the message clearly says 400 Bad Request, can even see in the message:

"message":"Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format."

this should ring the bell.

You are sending a plain text as your payload which is not accepted by your calling API. Change it into proper JSON format. Something like:

{
    "field1": "value1",
    "field2": "value2"
}
Asgar
  • 1,920
  • 2
  • 8
  • 17