Spring Boot WebClient将基本GraphQl邮政请求发送为RAW String

发布于 2025-02-13 11:03:15 字数 1649 浏览 0 评论 0原文

我正在尝试使用webclient来获取基本查询,以发送到我的GraphQl Server,我有以下内容:

String query = "{ query: 'query testQuery { testData }' }";
String response = WebClient.builder().build().post().uri("http://localhost:4020/graphql")
                .bodyValue(query).retrieve().bodyToMono(String.class).block();

这始终从POST 响应中产生400不良请求。

但是,在级别的一侧,我没有看到请求主体中的任何内容,这是我的节点服务器(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: {}

我尝试了许多不同的组合来使其工作,但无济于事。

为了确保API适用于此调用,以下curl命令作品:

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

,这似乎与this

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();

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文