500 when attempting to post to chatbase using restTemplate

146 views Asked by At

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.

3

There are 3 answers

0
Confused Dev On BEST ANSWER

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.

0
Viknesh On

I'm not familiar with MultiValueMap, but is it possible that it's serializing the keys in an unexpected way (possibly putting the values in a list)? I noticed that our code would currently cause that to return a 500 and am patching that now.

Would it be possible to provide the JSON payload of your request?

Otherwise, if that doesn't work for you, please reach out to [email protected] with your API key and we can investigate further.

0
Sean Pearson On

After further investigation, it was determined that MultiValueMap passes the fields as a list which was causing the error.