I'm trying to get even a basic query to send to my graphql server using WebClient
, I have the following:
String query = "{ query: 'query testQuery { testData }' }";
String response = WebClient.builder().build().post().uri("http://localhost:4020/graphql")
.bodyValue(query).retrieve().bodyToMono(String.class).block();
This is always producing a 400 Bad Request from POST
response.
On the sever side, however, I am not seeing anything in the request body, here is the debugging output from my node server (ApolloServer):
[1] headers: {
[1] 'accept-encoding': 'gzip',
[1] 'user-agent': 'ReactorNetty/1.0.20',
[1] host: 'localhost:4020',
[1] accept: '*/*',
[1] 'content-type': 'text/plain;charset=UTF-8',
[1] 'content-length': '49'
[1] }
[1] baseUrl: /graphql
[1] body: {}
I have tried a number of different combinations to get this to work, to no avail.
In order to ensure the API is functioning for this call, the following curl
command works:
curl --request POST \
--header 'content-type: application/json' \
--url http://localhost:4020/graphql \
--data '{"query":"query testQuery { testData }" }'
[1] headers: {
[1] host: 'localhost:4020',
[1] 'user-agent': 'curl/7.79.1',
[1] accept: '*/*',
[1] 'content-type': 'application/json',
[1] 'content-length': '41'
[1] }
[1] baseUrl: /graphql
[1] body: { query: 'query testQuery { testData }' }
Edit
So this seems to be related to the this github issue, there are 2 things, 'application/json' is required and rawbytes must be used to send strings. The following code works:
String query = "{ \"query\": \"query testQuery { testData }\" }";
String response = WebClient.builder().build().post().uri("http://localhost:4020/graphql")
.contentType(MediaType.APPLICATION_JSON).bodyValue(query.getBytes()).retrieve()
.bodyToMono(String.class).block();