How to make requests to Rasa API with Python's requests library?

I installed the rasa-demo code sample. For turning on the rasa actions I did:

user@User:~/rasa-demo ‹master*›$ rasa run actions --actions actions
2020-06-15 21:35:49 INFO     rasa_sdk.endpoint  - Starting action endpoint server...
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_explain_sales_form'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_explain_faq'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_set_faq_slot'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_pause'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_store_unknown_product'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_store_unknown_nlu_part'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_store_bot_language'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_store_entity_extractor'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_set_onboarding'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_store_problem_description'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_greet_user'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_default_ask_affirmation'.
2020-06-15 21:35:50 INFO     rasa_sdk.executor  - Registered function for 'action_default_fallback'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'action_get_community_events'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'action_next_step'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'action_docs_search'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'action_forum_search'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'action_tag_feedback'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'action_tag_docs_search'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'subscribe_newsletter_form'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'sales_form'.
2020-06-15 21:37:06 INFO     rasa_sdk.executor  - Registered function for 'suggestion_form'.

Then in the same folder, I can test with the rasa shell as follows:

user@User:~/rasa-demo ‹master*›$ rasa shell
2020-06-15 21:47:44 INFO     root  - Starting Rasa server on http://localhost:5005
2020-06-15 21:47:50 INFO     absl  - Using /var/folders/h5/9rj1zn8x4s59bk_mg_ktzv740000gn/T/tfhub_modules to cache modules.
2020-06-15 21:48:20 INFO     root  - Rasa server is up and running.
Bot loaded. Type a message and press enter (use '/stop' to exit):
Your input ->  hi
Hey there, my name is Sara.
By chatting to me you agree to our [privacy policy](https://rasa.com/privacy-policy/).
If you're new to Rasa, I can help you get started! If you've already started building an assistant, you can also ask me about the different parts of Rasa 🐦

How can I query as an API the chatbot? That is, instead of using the shell, I would like be aple to make a request and do the conversation through requests. So far I tried making a curl to the rasa server:

user@User:~/rasa-demo ‹master*›$ curl --request POST \
  --url http://localhost:5005/webhooks/rest/webhook \
  --header 'content-type: application/json' \
  --data '{
  "message": "Hello"
}'

UPDATE

I also tried this:

In:

import requests

response = requests.get('http://localhost:5005/webhooks/rest/webhook') 
print(response) 
print(response.headers) 
print(response.content)

Out:

<Response [405]>
{'Connection': 'keep-alive', 'Keep-Alive': '5', 'Allow': 'POST', 'Access-Control-Allow-Credentials': 'true', 'Content-Length': '60', 'Content-Type': 'text/plain; charset=utf-8'}
b'Error: Method GET not allowed for URL /webhooks/rest/webhook'

However, it is not working. What is the correct way to request Rasa as an API?

You can use the RESTChannel for that, as described on this docs page.

Basically, just add rest: as a line to your credentials.yml file. Then you can execute rasa run in your terminal and send messages to POST /webhooks/rest/webhook. Remember to include not only "message" but also "sender" in the JSON you post.

Still having the same issue. However, when I do:

curl localhost:5005/model/parse -d '{ "text": "hi how are you?" }'

I get the response with no issues:

{"intent":{"name":"chitchat","confidence":0.9999998807907104},"entities":[],"intent_ranking":[{"name":"chitchat","confidence":0.9999998807907104},{"name":"greet","confidence":1.4377799573139782e-7},{"name":"affirm","confidence":1.5398486041817705e-8},{"name":"technical_question","confidence":6.435655119929606e-9},{"name":"explain","confidence":5.8295870353219925e-9}],"response_selector":{"out_of_scope":{"response":{"name":"I can't help you with that, I'm sorry.","confidence":1.0},"ranking":[{"name":"I can't help you with that, I'm sorry.","confidence":1.0,"full_retrieval_intent":"out_of_scope\/other"},{"name":"I only understand English, I'm sorry.","confidence":0.0,"full_retrieval_intent":"out_of_scope\/non_english"}],"full_retrieval_intent":"out_of_scope\/other"},"faq":{"response":{"name":"The Rasa machine learning framework is open source! We strive to provide well-maintained and documented open source tools, allowing all makers to build the best AI assistants without friction. In addition to that,  we offer the free toolset [Rasa X](https:\/\/rasa.com\/docs\/rasa-x\/) and paid Rasa Enterprise   subscriptions. You can find out more [here](https:\/\/rasa.com\/product\/pricing\/)  about the plans and pricing.","confidence":0.6405603289604187},"ranking":[{"name":"The Rasa machine learning framework is open source! We strive to provide well-maintained and documented open source tools, allowing all makers to build the best AI assistants without friction. In addition to that,  we offer the free toolset [Rasa X](https:\/\/rasa.com\/docs\/rasa-x\/) and paid Rasa Enterprise   subscriptions. You can find out more [here](https:\/\/rasa.com\/product\/pricing\/)  about the plans and pricing.","confidence":0.6405603289604187,"full_retrieval_intent":"faq\/opensource_cost"},{"name":"You can connect Rasa to any channel you like, including voice channels. You can find more information [here](https:\/\/rasa.com\/docs\/core\/connectors\/).","confidence":0.086408831179142,"full_retrieval_intent":"faq\/voice"},{"name":"Rasa X is a toolset that helps you leverage conversations to improve your assistant. Learn more about Rasa X [here](https:\/\/rasa.com\/docs\/rasa-x\/) or read the blog post about why we developed it [here](https:\/\/blog.rasa.com\/algorithms-alone-wont-solve-conversational-ai-introducing-rasa-x\/).","confidence":0.07348804920911789,"full_retrieval_intent":"faq\/rasax"},{"name":"Rasa Masterclass episodes 2-4 focus on NLU. Check out episode 2 [here](https:\/\/www.youtube.com\/watch?v=k5UeywXA28k).","confidence":0.036000851541757584,"full_retrieval_intent":"faq\/tutorialnlu"},{"name":"Rasa Open Source is written in Python, but programming is not required to develop an assistant. To learn how to create an assistant, check out the [Rasa Masterclass](https:\/\/www.youtube.com\/watch?v=rlAQWbhwqLA). There is one place where programming skills come in handy, and that is when implementing [custom actions](https:\/\/rasa.com\/docs\/rasa\/core\/actions\/#custom-actions). You can create an action server in Node.js, .NET, Java, or any other language and define your actions there. For custom actions written in Python, we provide a small [Python SDK](https:\/\/rasa.com\/docs\/rasa\/api\/rasa-sdk\/#rasa-sdk) to make development even easier.","confidence":0.033165186643600464,"full_retrieval_intent":"faq\/is_programming_required"},{"name":"NLU\u2019s job is to interpret messages, and Core\u2019s job is to decide what should happen next.","confidence":0.0328323133289814,"full_retrieval_intent":"faq\/differencecorenlu"},{"name":"Rasa has more than 7000 community members and over 300 code and content contributors \ud83d\ude80.","confidence":0.030574288219213486,"full_retrieval_intent":"faq\/community_size"},{"name":"Rasa Enterprise is an enterprise-ready subscription to develop and ship contextual assistants at scale, which comes features for increased security and privacy, as well as expert support.","confidence":0.023307308554649353,"full_retrieval_intent":"faq\/ee"},{"name":"You can use Rasa to build assistants in any language you want!","confidence":0.022228408604860306,"full_retrieval_intent":"faq\/languages"},{"name":"We have a [comprehensive list of supported connectors](https:\/\/rasa.com\/docs\/core\/connectors\/), but if you don't see the one you're looking for, you can always create a custom connector by following [this guide](https:\/\/rasa.com\/docs\/rasa\/user-guide\/connectors\/custom-connectors\/).","confidence":0.021434469148516655,"full_retrieval_intent":"faq\/channels"}],"full_retrieval_intent":"faq\/opensource_cost"},"chitchat":{"response":{"name":"I'm great! Thanks for asking.","confidence":1.0},"ranking":[{"name":"I'm great! Thanks for asking.","confidence":1.0,"full_retrieval_intent":"chitchat\/ask_howdoing"},{"name":"Why are eggs not very much into jokes? -Because they could crack up.","confidence":4.659555519204162e-10,"full_retrieval_intent":"chitchat\/telljoke"},{"name":"Yep, I'm a bot!","confidence":3.358354705440547e-10,"full_retrieval_intent":"chitchat\/ask_isbot"},{"name":"It's the most wonderful time of the year!","confidence":1.3222738182161464e-10,"full_retrieval_intent":"chitchat\/ask_time"},{"name":"I hope you are being yourself.","confidence":8.436074061535237e-11,"full_retrieval_intent":"chitchat\/ask_whoami"},{"name":"Likewise!","confidence":7.619307962336563e-11,"full_retrieval_intent":"chitchat\/nicetomeetyou"},{"name":"I was built with a lot of love and patience.","confidence":4.3213418954302085e-11,"full_retrieval_intent":"chitchat\/ask_howbuilt"},{"name":"I'm not a human, I'm a bot! \ud83e\udd16","confidence":3.7382690692977505e-11,"full_retrieval_intent":"chitchat\/ask_ishuman"},{"name":"I'm Sara, the Rasa bot! At the same time I'm also the Rasa mascot \ud83d\udc26","confidence":2.4025663403204334e-11,"full_retrieval_intent":"chitchat\/ask_whoisit"},{"name":"Old enough to be a bot!","confidence":1.517344583312763e-11,"full_retrieval_intent":"chitchat\/ask_howold"}],"full_retrieval_intent":"chitchat\/ask_howdoing"}},"text":"hi how are you?"}

However, when I try with python’s request:

In:

import requests 
import json
payload = {'text':'hi how are you?'}
headers = {'content-type': 'application/json'}
r = requests.post('http://localhost:5005/model/parse', params=payload, headers=headers)
r.text

Out:

'{"version":"1.10.3","status":"failure","message":"No text message defined in request_body. Add text message to request body in order to obtain the intent and extracted entities.","reason":"BadRequest","details":{},"help":null,"code":400}'

Any idea how to make request the API with python requests? I would like to being able to do that because I will add logic that use the bot response in my app.

Note:

My credentials.yml looks like this:

# This file contains the credentials for the voice & chat platforms
# which your bot is using.
# https://rasa.com/docs/core/connectors/

rest:
#  # you don't need to provide anything here - this channel doesn't
#  # require any credentials

rasa:
  url: "http://localhost:5005"

callback:
  # URL to which Core will send the bot responses
  url: "http://localhost:5034/bot"

When I check the status of the server I get this:

did you get any further with this?
it seems odd that you can do the request with cURL and no credentials are needed.

Does the payload have to be pre-encoded as JSON?

oh yeah you’re doing params=payload, i think that’s just for adding URL params for a GET request how about json=payload

Error: 'text' might mean a key is missing? unhelpful message though.

payload = {'text':'hi how are you?'}
headers = {'content-type': 'application/json'}
r = requests.post('http://localhost:5005/model/parse', json=payload, headers=headers)

or use the json module to convert the object to json before you send it.

jsonData = json.dumps(payload)
r = requests.post('http://localhost:5005/model/parse', json=jsonData, headers=headers)

also sometimes you can add accepts

newHeaders = {'Content-type': 'application/json', 'Accept': 'text/plain'}
payload = {'text':'hi how are you?'}
headers = {'content-type': 'application/json'}
r = requests.post('http://localhost:5005/model/parse', json=payload, headers=headers)

This one worked. Seems that I confused the arguments of the post. Thanks!

1 Like

@dcsan Please I would like to know the same for the action_server on http://0.0.0.0:5055

Context: I have a form to be validated ask name, phone number on the actions side

When I try a post request on Webhook endpoint, I have the below error

2023-11-11 20:57:35 WARNING  rasa_sdk.executor  - Received an action call without an action.```