2

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();
fairidox
  • 3,358
  • 2
  • 28
  • 29
  • 400 means your request is faulty, its impossible for us to help you since you have posted absolutely no info at all what your api looks like or the description of your api – Toerktumlare Jul 04 '22 at 22:48
  • @Toerktumlare For brevity I don't see why anyone would need the API specification, the query posted is a valid graphql query, you would think would suffice to say. Even if it wasn't it does not explain why `WebClient` is sending an empty `{}` instead of whatever string I put in `bodyValue`. Nonetheless, I added a curl command output for clarity. – fairidox Jul 05 '22 at 00:31

0 Answers0