Using postman everything is fine with raw JSON content type.
{"query":"{getX(filter: \"X\", filter2: \"Y\") {data1, data2, data3} }
When invoking in the java client and I get 400 error.
String body = "{\"query\":\"{getX(filter: \\\"X\\\", filter2: \\\"Y\\\") {data1, data2, data3}}\"}";
// simply copied from postman to Intellij and it did some escaping for me
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity<>(body, headers);
ResponseEntity<List<SomeObject>> response = restTemplate.exchange(uri, POST, httpEntity, new ParameterizedTypeReference<List<SomeObject>>(){});
On server side I can see that I'm getting error:
JSON parse error: Cannot construct instance of 'java.util.LinkedHashMap': no String-argument constructor/factory method to deserialize from String value ....
//Looks like server thinks I passed one big string
... payload="{\"query\":\"{getX(filter: \\\"X\\\", filter2: \\\"Y\\\") {data1, data2, data3}}\"}" ...
But on server side when I'm invoking from postman I'm getting this payload log which works just fine
... payload={"query":"{getX(filter: \"X\", filter2: \"Y\") {data1, data2, data3}}"} ...
Obviously I can see that when I'm invoking from java payload data is wrapped inside qoutes and every qoute is escaped inside it, meanwhile from postman payload is not wrapped under qoutes and nothing is escaped except filters. What solution exactly could I do, using restTemplate?
I found the solution, basically all I needed is to put "query" as a key in a map and else in the value. In this case body does not get weirdly wrapped in qoutes.
Map<String, String> body = of("query", "{getX(filter: \"X\", filter2: \"Y\") {data1, data2, data3}}");
HttpEntity httpEntity = new HttpEntity<>(body, headers);
//and pass it to restTemplate...