CORS parameter at NLU API

What’s the supposed format for the value of --cors option?

I tried the domain name and also “*”, with an w/o square brackets but didn’t get to send a successful POST request.

My best option now is " --cors ‘*’ ". I can send a GET /status with that config but the POST request returns a 500 error with this info at server log:

     Traceback (most recent call last):
	  File "/home/marcis/.local/lib/python3.6/site-packages/rasa_nlu/server.py", line 142, in decorated
	    return f(*args, **kwargs)
	  File "/home/marcis/.local/lib/python3.6/site-packages/rasa_nlu/server.py", line 125, in decorated
	    return f(*args, **kwargs)
	  File "/home/marcis/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1613, in unwindGenerator
	    return _cancellableInlineCallbacks(gen)
	  File "/home/marcis/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1529, in _cancellableInlineCallbacks
	    _inlineCallbacks(None, g, status)
	--- <exception caught here> ---
	  File "/home/marcis/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
	    result = g.send(result)
	  File "/home/marcis/.local/lib/python3.6/site-packages/rasa_nlu/server.py", line 229, in parse
	    request.content.read().decode('utf-8', 'strict'))
	  File "/home/marcis/.local/lib/python3.6/site-packages/simplejson/__init__.py", line 518, in loads
	    return _default_decoder.decode(s)
	  File "/home/marcis/.local/lib/python3.6/site-packages/simplejson/decoder.py", line 370, in decode
	    obj, end = self.raw_decode(s)
	  File "/home/marcis/.local/lib/python3.6/site-packages/simplejson/decoder.py", line 400, in raw_decode
	    return self.scan_once(s, idx=_w(s, idx).end())
	simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Thank you!

can you show the command that you are running?

At the server:

python3 -m rasa_nlu.server --path my-project/models/ --cors '*'

At the web browser (Chrome), a jQuery call like tihs:

$.ajax({
    type: "POST",
    url: "http://localhost:5000/parse",
    data: {
        "q": "my question",
        "project": "my-project",
        "model": "my-model"
    },
    crossDomain: true,
    success: function (data, status, jqXHR) {
         console.log(data)
    }

})

Since the GET request to /status perfectly works:

$.ajax({
         url: "http://localhost:5000/status",
         crossDomain: true,
         success: function (data, status, jqXHR) {
             console.log(data)
         }
})

The POST request works locally if I call it with curl:

curl -XPOST localhost:5000/parse -d '{"q":"my question", "project":"my-project", "model":"my-model"'}

hey @marcis, the first thing you need to change is the command, you are running to start the nlu server i.e related to CORS it should be in double quotes whereas you have given it in single quote ,

python3 -m rasa_nlu.server --path my-project/models/ --cors "*"

The reason behind you are facing the problem in the jQuery call is your body part of the ajax call,you need to stringify it and then send the request :

$.ajax({
    type: "POST",
    url: "http://localhost:5000/parse",
    data:JSON.stringify( {
        "q": "my question",
        "project": "my-project",
        "model": "my-model"
    }),
    crossDomain: true,
    success: function (data, status, jqXHR) {
         console.log(data)
    }

}) I have tried the above code initially, i too faced the same issue while trying out your code,but i fixed it :wink:

Hope this helps… :slight_smile:

Yes, it works! THANK YOU!!!

Welcome @marcis :grin: