I’m using Spring Boot 2.7 and Java 11 HTTP Client to do a GraphQL Request like this:
private HttpRequest gqlRequest() {
URI uri = URI.create(dataUrl);
return HttpRequest.newBuilder()
.uri(uri)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, "Bearer " + sessionToken)
.POST(HttpRequest.BodyPublishers.ofString(gqlRequest))
.build();
}
When my gqlRequest is a Java Text Block it does not work. I get a 400 Bad Request
.
private final String gqlRequest = """
query ChargingStationAndConnector{
connector(id: "05f113a5-d662-4f00-bf31-fc18e10a6306"){
id
evseId
connectorType{
id
chargePointType
}
}
station:cp(id: "33842d46-3b53-4f7f-b2a9-cc062658e8c7"){
id
address
city
zip
country
}
}""";
However when i put this GQL into one line and add the tabulation and new line chars then it works.
String gqlRequest = "{"query":"query ChargingStationAndConnector{\n\tconnector(id: \"05f113a5-d662-4f00-bf31-fc18e10a6306\"){\n\t\tid\n\t\tevseId\n\t\tconnectorType{\n\t\t\tid\n\t\t\tchargePointType\n\t\t}\n\t}\n\tstation:cp(id: \"33842d46-3b53-4f7f-b2a9-cc062658e8c7\"){\n\t\tid\n\t\taddress\n\t\tcity\n\t\tzip\n\t\tcountry\n\t}\n}","operationName":"ChargingStationAndConnector"}";
Why and can i work with a theblock due to it’s much better readability?