简体   繁体   English

如何使用查询和变量从 java HttpClient 调用 GraphQL api?

[英]How to call GraphQL api from java HttpClient with query and variables?

I'm trying to call GraphQL API post endpoint with query and variables.我正在尝试使用查询和变量调用 GraphQL API端点。 Query and variables both add as JSON object.查询和变量都添加为 JSON object。 But return "Invalid request".但返回“无效请求”。

I've read through numerous questions here on Stack overflow, but I did not encounter the problem I'm having.我在这里阅读了许多关于堆栈溢出的问题,但我没有遇到我遇到的问题。

Java sample Java样品

public class main3 {
    public static void main(String[] args) throws IOException {
        String line, queryString, url;
        url = "https://search-sandbox.sample.com/graphql";
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;

        client = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);

        httpPost.addHeader("Authorization", "Basic VG91**");
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-Type", "application/json");

          try {
            
                String query = "query getPropertiesByIds($SearchCriteriaByIds: SearchCriteriaByIdsInput) {\n"
                + "  getPropertiesByIds(searchCriteriaByIds: $SearchCriteriaByIds) {\n"
                + "  properties {\n"
                + "      propertyId\n"
                + "    }\n"
                + "  }\n"
                + "}";
        
                String variable = "{\n"
                + " \"SearchCriteriaByIds\": {\n"
                + "     \"propertyIds\": [\n"
                + "         134388,\n"
                + "         424023,\n"
                + "         134388,\n"
                + "         22549064\n"
                + "     ]\n"
                + " }\n"
                + "}";
            
            Map<String, Object> variables = new HashMap<>();
            
            variables.put("query", query);
            variables.put("variables", variable);
            
            JSONObject jsonobj; 
            jsonobj = new JSONObject(variables);


            StringEntity entity = new StringEntity(jsonobj.toString());
            httpPost.setEntity(entity);

            
            response = client.execute(httpPost);

            BufferedReader bufReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder builder = new StringBuilder();

            while ((line = bufReader.readLine()) != null) {
                builder.append(line);
                builder.append(System.lineSeparator());
            }

            System.out.println(builder);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Return error code -返回错误代码 -

{"errorCode":1006,"errorMessage":"Invalid request.","details":null}

Please assist me to find out the issues.请协助我找出问题。

Variable must be json string but not in "".变量必须是 json 字符串,但不在“”中。

and take request as json like below Graphql Request并将请求作为 json 如下 Graphql 请求

String str= { "query": "query branchLocator ($filterCriteria: [FilterCriteria],$sortBy: SortBy){branchLocator (filterCriteria: $filterCriteria, offset: 0, limit: 20, sortBy: $sortBy) { postalCode latitude longitude addId1 addName1 addAddress1 addCity1 distance1 addId2 addName2 addAddress2 addCity2 distance2 addId3 addName3 addAddress3 addCity3 distance3 addId4 addName4 addAddress4 addCity4 distance4 addId5 addName5 addAddress5 addCity5 distance5 addId6 addName6 addAddress6 addCity6 distance6}}", "variables": {"filterCriteria":[{"key":"postalCode","filterInput":{"filterType":"eq","filterValues":["123456"]}}]} String str= { "query": "查询 branchLocator ($filterCriteria: [FilterCriteria],$sortBy: SortBy){branchLocator (filterCriteria: $filterCriteria, offset: 0, limit: 20, sortBy: $sortBy) { postalCode latitude longitude addId1 addName1 addAddress1 addCity1 distance1 addId2 addName2 addAddress2 addCity2 distance2 addId3 addName3 addAddress3 addCity3 distance3 addId4 addName4 addAddress4 addCity4 distance4 addId5 addName5 addAddress5 addCity5 distance5 addId6 addName6 addAddress6 addCity6 distance6}}”,“变量”:{“filterCriteria”:[{“key”:“postalCode ","filterInput":{"filterType":"eq","filterValues":["123456"]}}]}

} }

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

相关问题 如何使用 Java 代码调用 GraphQL 查询/变异 - How to call a GraphQL query/mutation with Java code 如何从 java Z91F1F487C7FB768642B3EB98F683BF5 应用程序调用 GraphQL api? 是否有任何注释支持 graphQL 查询格式? - How to invoke GraphQL api from a java spring-boot application? Is there any annotation which supports graphQL query formation? 使用 java 中的 apache `httpclient` 对 `graphql` 端点进行 `REST` 调用 - Making a `REST` call to `graphql` endpoint using apache `httpclient` in java 如何使用HttpUrlConnect用Java查询Github graphql API - How to query Github graphql API with java using HttpUrlConnect GitHub的GraphQL API如何从Java中使用? - How can GitHub's GraphQL API can be consumed from Java? 如何将表单数据变量传递给独立 java 代码的 rest api 调用 - How to pass Form data variables to the rest api call from stand alone java code 从Java中添加GraphQL查询中的片段 - Adding fragments in GraphQL query from Java 在Java中尝试使用HttpClient发送HTTP Post GraphQL查询时使用HTTP 400 - HTTP 400 When Trying to Send HTTP Post GraphQL Query using HttpClient in Java 如何从Java使用GraphQL? - how to use GraphQL from Java? 如何在 Java 中以编程方式创建/生成 GraphQL 查询 - How to create/generate GraphQL query programatically in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM