0

I have a POST API written in Restlet framework which accepts the data in org.restlet.representation.Representation 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 HTTPHeaders to send the data but the API is not accepting the values, all the fields the API is showing as NULL. 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.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.add("userid", userid);
    headers.add("usertype", usertype);
    headers.add("username", username);
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
    System.out.println(result);

And the service is like this:

@Post
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");
    System.out.println(userid);
    System.out.println(usertype);
    System.out.println(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 userid, now how to call it from Spring project? Am I doing something wrong?

Shivam
  • 649
  • 2
  • 8
  • 20
  • did you try it with curl and if yes can you share (or export) the curl example command? thanks in advance – Daniel Bubenheim Sep 29 '16 at 15:07
  • No I didn't tried it with curl – Shivam Sep 29 '16 at 15:14
  • It could help a lot to analyse and further understand the cause. It's easy to export it via postman. See this: https://www.getpostman.com/docs/creating_curl – Daniel Bubenheim Sep 29 '16 at 15:49
  • curl command will not help me, I think there is some issue with the data I am sending. I want to know in what manner it should be sent. – Shivam Sep 29 '16 at 16:34
  • I came up with curl since it's plain and simple and you can easily share it. If that works you know that your basic assumptions on how the request should be constructed are correct. With this knowledge you can go on fixing your code. Without this information it's hard - at least for me - to follow your example and find a solution. – Daniel Bubenheim Sep 29 '16 at 17:03
  • @DanielBubenheim I have updated the question with the curl command – Shivam Oct 03 '16 at 13:46
  • In your code you have `MediaType.APPLICATION_JSON`, try to change it to `MediaType.APPLICATION_FORM_URLENCODED_TYPE` – Hrabosch Oct 03 '16 at 13:53
  • @Hrabosch I have already tried using MediaType.APPLICATION_FORM_URLENCODED with no Luck – Shivam Oct 03 '16 at 13:57
  • @Shivam Did you try it with `_TYPE` suffix? – Hrabosch Oct 03 '16 at 14:03
  • _TYPE isn't found, there is _VALUE but that is String in the MediaType class. /** * Public constant media type for {@code application/x-www-form-urlencoded}. * */ public final static MediaType APPLICATION_FORM_URLENCODED; – Shivam Oct 03 '16 at 14:24
  • And from this post I am able to make it work :) http://forum.spring.io/forum/spring-projects/web/70845-sending-post-parameters-with-resttemplate-requests – Shivam Oct 03 '16 at 14:25

2 Answers2

0

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<?>>();
messageConverters.add(stringHttpMessageConverternew);
messageConverters.add(formHttpMessageConverter);
restTemplate.setMessageConverters(messageConverters);
System.out.println(restTemplate.postForObject(uri, params, String.class));

In my case I am also getting the expected result If I exclude the following code:

HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
HttpMessageConverter stringHttpMessageConverternew = new StringHttpMessageConverter();
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(stringHttpMessageConverternew);
messageConverters.add(formHttpMessageConverter);
restTemplate.setMessageConverters(messageConverters);
Shivam
  • 649
  • 2
  • 8
  • 20
0

@Shivam Thanks for updating the question. With the curl command in place I now see that the data you basically wanted to send is inside the request's body. Therefore the first approach with HttpHeaders won't work. Here's an example of how it could look like for your first approach using the exchange method of Springs RestTemplate:

@Test
public void test() {
    RestTemplate restTemplate = new RestTemplate();
    final String uri = "http://localhost:8080/adduser";
    String userid = "05580a6caa7244a6986ca834403f1a93";
    String usertype = "buyer";
    String username = "shivam42";

    // create request body
    JSONObject request = new JSONObject();
    request.put("userid", userid);
    request.put("usertype", usertype);
    request.put("username", username);

    // set headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    HttpEntity<String> entity = new HttpEntity<>(request.toString(), headers);

    ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);
    System.out.println(result.getBody());
}

This should also work as expected and return the userid. See also POST request via RestTemplate in JSON for further information.

Community
  • 1
  • 1
Daniel Bubenheim
  • 4,043
  • 3
  • 14
  • 20
  • I tried it, but it is not giving the result as expected. The web service getting Null for all the 3 parameters. – Shivam Oct 04 '16 at 11:08