简体   繁体   English

Spring GraphQL HttpGraphQlClient - 需要知道确切的请求正文

[英]Spring GraphQL HttpGraphQlClient - Need to know the exact request body

I'm using spring-boot-starter-graphql .我正在使用spring-boot-starter-graphql I'm communicating with a GraphQL endpoint residing on another server.我正在与驻留在另一台服务器上的 GraphQL 端点通信。 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.除了不记名令牌头之外,我还使用 GraphQL 端点生成另一种令牌头,内部称为 PoP 令牌。 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.该令牌由 http 方法、承载令牌和请求正文生成。

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.我正在从一个类似于我最好的猜测的字符串生成这个令牌,这可能是由HttpGraphQlClient生成的 JSON 作为下面请求中的请求正文。

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.我在 localhost:8080 上运行来自 Eclipse 的代码。 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.不幸的是,如果我将以下字符串(以及我尝试过的其他类似变体)传递给生成 PoP 令牌的方法,则生成的令牌将是不正确的。

The requests content-type is application/json.请求内容类型为 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.但是我需要确切地知道请求正在形成什么 JSON 主体,以便端点可以验证请求主体。

Is there some way I can see exactly what JSON body is being passed to the destination?有什么方法可以让我准确地看到传递到目的地的 JSON 正文是什么? I am getting a GraphQlTransportException with a nested WebClientResponseException but neither of these objects are telling me what the exact request body is.我得到一个带有嵌套WebClientResponseExceptionGraphQlTransportException但这些对象都没有告诉我确切的请求正文是什么。 Thanks谢谢

The HttpGraphQlClient uses the Spring class org.springframework.web.reactive.function.client.webclient to perform the http calls. HttpGraphQlClient使用 Spring 类org.springframework.web.reactive.function.client.webclient来执行 http 调用。 You can provide your own webclient instance when you create your HttpGraphQlClient:您可以在创建 HttpGraphQlClient 时提供自己的 webclient 实例:
https://docs.spring.io/spring-graphql/docs/current-SNAPSHOT/api/org/springframework/graphql/client/HttpGraphQlClient.html 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这是关于如何记录 HTTP 调用的详尽教程,包括 Spring WebClient 的请求和响应主体: https ://www.baeldung.com/spring-log-webclient-calls

Providing the customized webclient when creating your HttpGraphQlClient should do the job.在创建 HttpGraphQlClient 时提供自定义的 webclient 应该可以完成这项工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM