1

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:

  1. 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
Alexandre
  • 751
  • 2
  • 11
  • 27
  • The `data=` part of it is not json. In the most basic situation, you can serialize it yourself. Or, you can use libraries like [GSON](https://code.google.com/p/google-gson/) or [Jackson](http://jackson.codehaus.org/) to serialize java objects. – Sotirios Delimanolis Apr 02 '13 at 19:35
  • 1
    Google for the JSON.org home page. (And bookmark it.) At the bottom are listed a dozen or so different packages for serializing/deserializing JSON in Java. Pick one you like. – Hot Licks Apr 02 '13 at 19:49
  • @HotLicks is bookmarking necessary? – chill appreciator Jun 24 '20 at 19:56
  • @standalone - If you're planning to do a lot of JSON stuff. – Hot Licks Jun 24 '20 at 20:12

0 Answers0