Home > Blockchain >  Spring GraphQL HttpGraphQlClient - Need to know the exact request body
Spring GraphQL HttpGraphQlClient - Need to know the exact request body

Time:06-25

I'm using spring-boot-starter-graphql. I'm communicating with a GraphQL endpoint residing on another server. In addition to a bearer token header I'm also generating another kind of token header with a GraphQL endpoint, internally called a PoP token. This encrypted token is used to verify that the request body hasn't been altered. This token is generated from the http method, the bearer token, and the request body.

I am generating this token from a string that resembles what is my best guess might be the JSON being generated by the HttpGraphQlClient as the request body in the request below.

Unfortunately I can't seem to find out exactly what request body is being generated from this call:

        String documentStr = "{\nphoneContactability0: phoneContactability(ruleId:\"2\", phoneNumber:\"1234567892\"){\nphone{\nphoneNumber\nruleValue\n}\n}}";
        Object response = httpGraphQlClient.mutate()
            .url(url)
            .headers(headers -> {
                headers.addAll(httpEntity.getHeaders());
            })
            .build()
            .document(documentStr)
            .retrieve("phoneContactability0")
            .toEntity(Object.class)
            .block();

I am running the code from Eclipse on localhost:8080. Unfortunately if I pass the below string (and other similar variants I've tried) to the method that generates the PoP token, the generated token will be the incorrect one.

The requests content-type is application/json. I think it is sending something along the lines of:

{"variables":null,"query":"{\nphoneContactability0: phoneContactability(ruleId:\"2\", phoneNumber:\"1234567892\"){\nphone{\nphoneNumber\nruleValue\n}\n}}"}

But I need to know exactly what JSON body is being formed by the request so that the endpoint can verify the request body.

Is there some way I can see exactly what JSON body is being passed to the destination? I am getting a GraphQlTransportException with a nested WebClientResponseException but neither of these objects are telling me what the exact request body is. Thanks

CodePudding user response:

The HttpGraphQlClient uses the Spring class org.springframework.web.reactive.function.client.webclient to perform the http calls. You can provide your own webclient instance when you create your HttpGraphQlClient:
https://docs.spring.io/spring-graphql/docs/current-SNAPSHOT/api/org/springframework/graphql/client/HttpGraphQlClient.html

Here is an exhaustive tutorial on how to log the HTTP calls, including request and response bodies for a Spring WebClient: https://www.baeldung.com/spring-log-webclient-calls

Providing the customized webclient when creating your HttpGraphQlClient should do the job.

  • Related