Search code examples
chatbase

500 when attempting to post to chatbase using restTemplate


I am able to post to Chatbase https://chatbase.com/api/message via SoapUI and curl requests. However when I attempt to post using restTemplate to the same endpoint using the same headers and same body I am getting a {"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);

I have tried everything I can think of, any help is appreciated.


Solution

  • Here is the implementation that ended up working for us.

        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);
    

    Thanks for the help Chatbase! Great response time via email and stack overflow.