如何解决在java中使用apache`httpclient`对`graphql`端点进行`REST`调用
我正在尝试对 graphql 端点进行简单的休息调用,但从服务器收到 400
错误。对此已经有一段时间了。
有效载荷
query = "{\n" +
" lookup(dob: \"01/01\",preSsn : { ssn: \"{actualvalue}\"})\n" +
" {\n" +
" items {\n" +
" individualCrdNumber\n" +
" firstName\n" +
" middleName\n" +
" lastName\n" +
" suffixName\n" +
" }\n" +
" }\n" +
"}";
错误信息:{"statusCode":400,"statusDescription":"Bad Request","message":"","messageDetails":[]}
代码:
HttpClient client = getHttpsClient();
HttpPost httpPost = new HttpPost(URL);
StringEntity input = new StringEntity(query,ContentType.APPLICATION_JSON);
httpPost.setEntity(input);
final String plainCred = "userName"+":"+"password";
final String base64Creds = Base64.getEncoder().encodeToString(plainCred.getBytes());
httpPost.addHeader("Authorization","Basic " + base64Creds);
httpPost.addHeader("Content-Type","application/json");
httpPost.addHeader("Accept","application/json");
httpPost.addHeader("Cache-Control","no-cache");
HttpResponse response = client.execute(httpPost);
解决方法
查询需要包含在分配给 query
元素的对象中。此对象还可以包含用于值绑定的 variables
元素:
{
"query": "...","operationName": "...","variables": { "myVariable": "someValue",... }
}
See the spec regarding GraphQL over HTTP using POST。
试试
String query =
"{" +
"\"query\" : \"query {"+
"lookup(dob: \"01/01\",preSsn : { ssn: \"{actualvalue}\"})" +
"{items{individualCrdNumber firstName middleName lastName suffixName}}" +
"}" +
"}";
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。