Rasa API giving bad request when used in JAVA

115 Views Asked by At

I am trying to build a chatbot application and want to call Rasa APIs via my SpringBoot application. I am using maven and client5 to do Http calls. Here's the dependency:

<dependency>
        <groupId>org.apache.httpcomponents.client5</groupId>
        <artifactId>httpclient5</artifactId>
        <version>5.2.1</version>
</dependency>

Java API code

@PostMapping("/chat-2")
    public String chat(@RequestParam String message) {
        String rasaEndpoint = "http://localhost:5005/webhooks/rest/webhook";
        try {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            ClassicHttpRequest httpPost = ClassicRequestBuilder.post(rasaEndpoint)
                    .addHeader("Content-Type", "application/json")
                    .setEntity(new UrlEncodedFormEntity(Arrays.asList(
                            new BasicNameValuePair("message","hi"))))
                    .build();
            httpclient.execute(httpPost, response -> {
                System.out.println(response.getCode() + " " + response.getReasonPhrase());
                final HttpEntity entity2 = response.getEntity();
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity2);
                return "part 1";
            });
        }
        catch(Exception e) {
            System.out.println("Error : " + e);
        }
        return "part 2";
    }

The above code is supposed to send "hi" to rasa and I should get back "Hey! How are you?" from the bot but instead I get a bad request. I am able to use Rasa API from Postman. I have atteched a screen shot below. enter image description here I am not sure what I am doing wrong or if some headers or parameters are missing in the code.

1

There are 1 best solutions below

0
PhilipAllStar On

Looks like an authentication error to me. Try starting Rasa with :

rasa run --enable-api --cors "*"