Spring Boot WebClient将基本GraphQl邮政请求发送为RAW String
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论