繁体   English   中英

使用 java 中的 apache `httpclient` 对 `graphql` 端点进行 `REST` 调用

[英]Making a `REST` call to `graphql` endpoint using apache `httpclient` in java

我正在尝试对 graphql 端点进行简单的 rest 调用,但从服务器收到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元素的 object 中。 这个 object 还可以包含一个variables元素用于值绑定:

{
  "query": "...",
  "operationName": "...",
  "variables": { "myVariable": "someValue", ... }
}

使用 POST 请参阅有关 GraphQL 而不是 HTTP 的规范

尝试

String query = 
        "{" +
          "\"query\" : \"query {"+ 
            "lookup(dob: \"01/01\", preSsn : { ssn: \"{actualvalue}\"})" +
            "{items{individualCrdNumber firstName middleName lastName suffixName}}" +
          "}" +
        "}";

暂无
暂无

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

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