1

I'm having some problems using the Spring restTemplate.

According to API documentation (ONLY FOR PHP) of Rest Service that i'm trying to access. The correct format of POST are:

curl -d "data={\"client_email\":\"steve@martiancraft.com\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}" http://www.setster.com/api/v2/company/7089/appointment?session_token=niab4ptf9mjjem41cooso389f3

to get that response:

{
"statusCode":0,
"statusDescription":"OK",
"data":{
    "status":2,
    "client_id":103352,
    "client_email":"steve@martiancraft.com",
    "client_name":"Steve Ryner",
    "company_id":"7089",
    "employee_id":9862,
    "location_id":"13832",
    "start_date":"2012-08-15 14:00",
    "end_date":"2012-08-15 15:00",
    "length":3600000,
    "note":"This is a test.",
    "service_id":20216,
    "type":"60 Min Swedish",
    "duration_padding":0,
    "repeat_type":0,
    "subservices":"",
    "timezone_dif":-18000,
    "price":"60",
    "custom_fields":"[]",
    "client_phone":"",
    "client_address":"",
    "payment_pending":0,
    "id":171302483
}

}

Here's the important part of my code:

RestTemplate restTemplate = new RestTemplate();

Map<String, String> data = new HashMap<String, String>();

        data.put("client_name", "Alexandre Moraes");
        data.put("client_email", "kalvinmoraes@gmail.com");
        data.put("client_phone", "98065867");
        data.put("employee_id", "0");
        data.put("location_id", "16675"); // Here i have to specify the "Location_ID" because there is more than just one
        data.put("start_date", "2013-05-03 09:15:00");
        data.put("service_id", "18499");

        String result = restTemplate.postForObject("http://setster.com/api/v2/company/6788/appointment/?session_token="+session_token, data, String.class);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.println(result);

But when i execute that postForObject, the response are a 400 Bad request as if something had sent wrong:

WARNING: POST request for "http://setster.com/api/v2/company/6788/appointment/?session_token=[censored]" resulted in 400 (Bad Request); invoking error handler
WARNING: StandardWrapperValve[setster]: PWC1406: Servlet.service() for servlet setster threw exception

Maybe the format i was sending the data are wrong. But i can't figure out how can i manage that information.

Anyone has any idea of what i'm doing wrong?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Alexandre
  • 751
  • 2
  • 11
  • 27

2 Answers2

1

In your curl request, the body of your HTTP POST is:

data={\"client_email\":\"steve@martiancraft.com\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}

So your endpoint is expecting a standard HTTP POST with a single string called data, with your serialized JSON. RestTemplate assumes you want to submit this way:

{\"client_email\":\"steve@martiancraft.com\",\"client_name\":\"Steve Ryner\",\"employee_id\":0,\"service_id\":20216,\"start_date\":\"2012-08-15 09:00:00\",\"note\":\"This is a test.\"}

i.e., just a JSON object. You won't be able to use RestTemplate for this. Use one of Spring's built-ins for HTTP services.

Peter Bratton
  • 6,302
  • 6
  • 39
  • 61
  • Thanks for the useful information @jordan002, actually this is the first time i see Java and it is a bit hard to understand some things: **1. What do you mean with "Spring Built-in for Http Services"?** Sorry. I'm not so good in English too so, can you give me a example? Thanks. – Alexandre Apr 02 '13 at 16:27
  • Added a link to the manual for Spring remoting. Hopefully this will help. If you're happy with the response, an accept or upvote would be greatly appreciated. – Peter Bratton Apr 02 '13 at 17:54
0

Use Jackson lib - is easy way to convert object to json:

ObjectMapper mapper = new ObjectMapper();

mapper.writeValue(resp.getWriter(), data);