500 尝试使用 restTemplate post 到 chatbase

500 when attempting to post to chatbase using restTemplate

我能够通过 SoapUI 和 curl 请求post 到 Chatbase https://chatbase.com/api/message。但是,当我尝试使用相同的 headers 和相同的 body 将 restTemplate post 用于相同的端点时,我得到了 {"reason": "Unknown server error.", "status": 500}

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("api_key", "myApiKey");
body.add("platform", "Web"); 
body.add("user_id", "1");
body.add("type", "user");`

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);

restTemplate.exchange("https://chatbase.com/api/message", HttpMethod.POST, request, Void.class);

我已经尝试了所有我能想到的方法,感谢任何帮助。

我不熟悉 MultiValueMap,但它是否可能以意想不到的方式序列化键(可能将值放入列表中)?我注意到我们的代码目前会导致 return 500,现在正在修补它。

是否可以提供您请求的 JSON 负载?

否则,如果这对您不起作用,请使用您的 API 密钥联系 chatbase-feedback@google.com,我们可以进一步调查。

经过进一步调查,确定 MultiValueMap 将字段作为导致错误的列表传递。

这是最终为我们工作的实现。

    Long stamp = System.currentTimeMillis();

    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

    JSONObject body = new JSONObject();
    body.put("api_key", key);
    body.put("type", user);
    body.put("platform", "Web");
    body.put("message", userMessage);
    body.put("intent", intentName);
    body.put("version", "1.0");
    body.put("user_id", userId);
    body.put("not_handled", notHandled);
    body.put("time_stamp", stamp);

    HttpEntity<Object> request = new HttpEntity<>(body, headers);

    String url = "https://chatbase.com/api/message";

    restTemplate.exchange(url, HttpMethod.POST, request, Void.class);

感谢 Chatbase 的帮助!通过电子邮件和堆栈溢出的快速响应时间。