我目前正在尝试使用 unirest for java 在对讲机 API 上执行 POST 请求,它返回服务器错误 500,但是当我与 Postman 执行相同的请求时,它会返回我想要的结果。
示例代码:
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
headers.put("Authorization", "Bearer XXXXXXXXXXXXXXXXXXXXXXX");
HttpResponse response = Unirest
.post("https://api.intercom.io/conversations/search")
.headers(headers)
.body(requestBody)
.asObject(HashMap.class);
这是我的请求正文
private Map<String, Object> getQueryValues(String field, String operator, Object value) {
Map<String, Object> map = new HashMap<>();
map.put("field", field);
map.put("operator", operator);
map.put("value", value);
return map;
}
private Map<String, Object> getRequestBody() {
List<Map<String, Object>> value = new ArrayList<>();
value.add(getQueryValues("updated_at", ">", 1590278400));
value.add(getQueryValues("source.delivered_as", "=", "admin_initiated"));
Map<String, Object> query = new HashMap<>();
query.put("operator", "AND");
query.put("value", value);
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("query", query);
return requestBody;
}
问题原来是我的 UNIX 日期长整型值,其中正确的值应该是大纪元秒。所以从myDate.getTime()
我把它改成了myDate.toInstant().getEpochSecond()
.