Search code examples
angularcorsrasa-nlurasa-core

Enabling CORS Support in Rasa Core HTTP API


I am experimenting with the default MoodBot example of Rasa Core framework. I have developed a simple Angular 5 app to serve as a front-end for the conversation flow. The Rasa HTTP API is run on a Linux-based back-end server at port 5005 (let's call it http://my.very.own.server:5005/) using the following command:

python3 -m rasa_core.server -d models/dialogue -u models/nlu/default/current -o out.log

I can communicate with the server using Postman application and get a valid response. But when I try the same from my Angular app, it returns a 404 (Not found) error.

Using postman (working):

POST: my.very.own.server:5005/conversations/default/parse
Body of the request: {"query":"hi"}
Headers: Content-Type: application/json
Response: Success (200) - Valid JSON content received

Using Angular app (not working):

public getResponse(query: string) {

  let finalUrl: string = "http://my.very.own.server/" + "conversations/default/parse/";

  let data = {
    'query' : query
  }
  return this.http.post(
      `${finalUrl}`, data
    ).map(res => {
      return res.json()
    })
}

// Call the method using:

getResponse("hi").subscribe(res => {
  console.log(res);
});

This results in a console error:

Failed to load http://my.very.own.server:5005/conversations/default/parse/:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:4200' is therefore not allowed access.
The response had HTTP status code 404.

What I have tried:

  1. I tried running the Rasa server with an additional --cors parameter (as seen here):

python3 -m rasa_core.server -d models/dialogue -u models/nlu/default/current -o out.log --cors ["*"]

  1. I tried adding "cors_origins" : ["*"] to the nlu_model_config.json file (as seen here).

Both ways made no difference. What am I doing wrong here, and what can I do to properly enable CORS support from the Rasa HTTP API?


Solution

  • We recently fixed this, so you'll need this PR . For now you'll have to clone master, but we're releasing a new version 0.9 soon so it'll be available on pypi as well.