Here is the deal.
I have a postForObject to sent. In that postForObject, i must send 2 objects:
- A JSON OBJECT WITH NAME "DATA" (e.g: data={"client":"name","id":"3"})
- A STRING WITH NAME "SESSION_TOKEN" (e.g: session_token="abc")
I've tried so many ways do this but i've always receive a 400 (BAD REQUEST) from server even though the same way being used with CURL in PHP. (I've checked the arguments that the curl php script sends and are exactly the two i described above)
So, here are my question:
- How can i manage/create these 2 arguments (JSON being serialized) and sent via postForObject? Can someone explain step by step this process? (So i can get a good understanding about the issue since i'm new to Java/Spring)
P.S.: I've already did multiple getForObject functions that worked fine. I really just need the way HOW can i pass these arguments (1 Json serialized and 1 String) with postForObject.
--------------------------------------------------
UPDATE (Send Post Arguments are now working)
I've success sending POST data via postForObject
Just create the RestTemplate like this:
RestTemplate restTemplate = new RestTemplate();
HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
HttpMessageConverter stringHttpMessageConverternew = new StringHttpMessageConverter();
and the attributes must be created with MultiValueMap like this:
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("session_token",session_token);
then, to finish, sent the postForObject like this:
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
String result = restTemplate.postForObject(API_URL + "company/6788/appointment/", map, String.class);
THE QUESTION NOW IS:
Which is the best way to serialize a JSON EXACTLY like this:
{"client_email":"kalvinmoraes@gmail.com","client_name":"Alexandre Moraes"}
to
%7B%22client_email%22%3A%22kalvinmoraes%40gmail.com%22%2C%22client_name%22%3A%22Alexandre%20Moraes%22%7D