How to POST form data with Spring RestTemplate?

I want to convert the following (working) curl snippet to a RestTemplate call:

curl -i -X POST -d "[email protected]" https://app.example.com/hr/email

How do I pass the email parameter correctly? The following code results in a 404 Not Found response:

String url = "https://app.example.com/hr/email";

Map<String, String> params = new HashMap<String, String>();
params.put("email", "[email protected]");

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity( url, params, String.class );

I’ve tried to formulate the correct call in PostMan, and I can get it working correctly by specifying the email parameter as a “form-data” parameter in the body. What is the correct way to achieve this functionality in a RestTemplate?

4 Answers
4

Leave a Comment