Failed when parsing body as json - call Rasa API with request.post()

I’m writing a function to call Rasa API for intent prediction. Here is my code:

def run_test():
    url = "http://localhost:5005/model/parse"
    obj = {"text": "What is your name?"}
    response = requests.post(url, data=obj)
    print(response.json())

I also start Rasa server with this command: rasa run -m models --enable-api --cors “*” --debug

And here is what I got from Rasa server terminal: enter image description here

In the terminal that I excuted run_test(), I got this result:

{'version': '2.7.1', 'status': 'failure', 'message': 'An unexpected error occurred. Error: Failed when parsing body as json', 'reason': 'ParsingError', 'details': {}, 'help': None, 'code'
: 500}

Anybody help me to solve this problem? Thank you very much!

@thuythao1912 Please, check the proper code for API with key-value and JSON format.

I know it’s a bit late, however for future documentation, let me help you out here.

You need to convert the object into a JSON string first, before sending it to the Rasa API. So use something like json.dumps(obj) first, e.g.,

requests.post('<your rasa parse API endpoint url>', data=json.dumps({'text': text}))
2 Likes

tks. I use json.dumps(obj) and it works.