Shivam Shivam - 1 year ago 92
reST (reStructuredText) Question

Calling RESTlet API from Spring Framework

I have a POST API written in Restlet framework which accepts the data in

form, I want to hit the service with some variables and there values from Spring project. How to do that?

Right now I am using the
to send the data but the API is not accepting the values, all the fields the API is showing as
. The code is as follows:

final String uri = "http://localhost:8080/MyServices/adduser";
String userid = "05580a6caa7244a6986ca834403f1a93";
String usertype = "buyer";
String username = "shivam42";
RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.add("userid", userid);
headers.add("usertype", usertype);
headers.add("username", username);
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

ResponseEntity<String> result =, HttpMethod.POST, entity, String.class);

And the service is like this:

public String newUser(Representation entity) {
Form form = new Form(entity);
String userid = form.getValues("userid");
String usertype = form.getValues("usertype");
String username = form.getValues("username");
return userid;

This is the code generated from curl Maybe someone can help me with this:

curl -X POST -H "Cache-Control: no-cache" -H "Postman-Token: 33e6a1c5-c1c9-694f-3d7f-26cbcea61870" -H "Content-Type: application/x-www-form-urlencoded" -d 'userid=05580a6caa7244a6986ca834403f1a93&usertype=buyer&username=shivam42' "http://localhost:8080/MyServices/adduser"

When I am calling the API from POSTMAN it is giving me the correct
, now how to call it from Spring project? Am I doing something wrong?

Answer Source

From the Spring forum I found the solution to this. Now my code is:

final String uri = "http://localhost:8080/MyServices/adduser";
String userid = "05580a6caa7244a6986ca834403f1a93";
String usertype = "buyer";
String username = "shivam42";
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("userid", userid);
params.add("usertype", usertype);
params.add("username", username);

RestTemplate restTemplate = new RestTemplate();
HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
HttpMessageConverter stringHttpMessageConverternew = new StringHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
System.out.println(restTemplate.postForObject(uri, params, String.class));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download