Http API POST request response with nothing

Hi everyone, I am new to rasa and http server. I met with a problem recently:

I tried to use Java language to send post request to my rasa server, although it works well with Get request, but for the POST request, rasa response with nothing.

I used rasa init to create a new project and didn’t change any files. Then I ran rasa by rasa run --enable_api

I tried post requests including Run an action in a conversation and Parse a message using the Rasa model. I sent Json same to the ones shown on the screen, and send message to http://xxx.xxx.xxx.xxx:5005//conversations/default/excute and http://xxx.xxx.xxx.xxx:5005//model/parse.

However, my rasa just send me nothing: no errors or message, but only empty.

The get request works well, and my rasa model runs well in the shell.

Do anyone have some ideas about the possible reasons? Any help will be appreciated!:pray:

Thanks!!

How does your request look like?

I’m using the latest Rasa version and followed the steps you mentioned. After I started the Rasa server, I send the following request to it:

curl -X POST \
  http://localhost:5005/model/parse \
  -H 'Content-Type: application/json' \
  -d '{
    "text": "Hello!"
}'

That request returns the following response:

{
    "intent": {
        "name": "greet",
        "confidence": 0.9682487249
    },
    "entities": [],
    "intent_ranking": [
        {
            "name": "greet",
            "confidence": 0.9682487249
        },
        {
            "name": "mood_great",
            "confidence": 0.0384795777
        },
        {
            "name": "deny",
            "confidence": 0
        },
        {
            "name": "affirm",
            "confidence": 0
        },
        {
            "name": "mood_unhappy",
            "confidence": 0
        },
        {
            "name": "goodbye",
            "confidence": 0
        }
    ],
    "text": "Hello!"
}

Also the other endpoint you mentioned returns a proper answer:

curl -X POST \
  'http://localhost:5005/conversations/default/execute?include_events=AFTER_RESTART' \
  -H 'Content-Type: application/json' \
  -d '{"name":"utter_greet"}'

Hi, Tanja. Thanks for your reply.

I tried the request the same with yours:

curl -X POST \
  http://localhost:5005/model/parse \
  -H 'Content-Type: application/json' \
  -d '{
    "text": "Hello!"
}'

But the request returns:

An error occurred while handling an error

However, if I send this request:

curl -X GET http://localhost:5005/

It returns the right response:

hello from Rasa: 0.15.0a1

Do you know why GET request works well but POST cannot? Thanks!!:pray:

I just found that, for the GET request:

curl -X GET http://localhost:5005/domain

It also returns a weird answer as follow:

{
  "version":"0.15.0a1",
  "status":"failure",
  "message":"Invalid Accept header. Domain can be provided as json \
                    (\"Accept: application\/json\") or yml (\"Accept: application\/x-yml\"). \ 
                    Make sure you've set the appropriate Accept header.",
  "reason":"InvalidHeader",
  "details":{},"help":null,"code":406
}

Do you have any idea about it? Thank you.

Can you try this instead:

curl -X GET -H "Accept: application/json" http://localhost:5005/domain

Yes it works. Thanks a lot, Ameid!

Could you please help me with the POST problems? :pleading_face:

Can you start the server in debug mode rasa run --enable-api --debug and check if there is any error printed to the console when you are making the POST request?

Hi Tanja, thanks a lot for your help!

I have solved some problems through your help, so now my rasa server works well when I use post and get request in the shell.

However, if I send the post request through Java language, still, the get request works well but POST can get no response. I used the debug mode and the bug information is:

2019-07-03 15:46:48 DEBUG    rasa.server  - Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/sanic/request.py", line 149, in load_json
    self.parsed_json = loads(self.body)
ValueError: Unexpected character found when decoding 'true'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/rasa/server.py", line 801, in parse
    data = emulator.normalise_request_json(request.json)
  File "/usr/local/lib/python3.6/site-packages/sanic/request.py", line 143, in json
    self.load_json()
  File "/usr/local/lib/python3.6/site-packages/sanic/request.py", line 153, in load_json
    raise InvalidUsage("Failed when parsing body as json")
sanic.exceptions.InvalidUsage: Failed when parsing body as json

There might be some problems with the entity of post request, but I cannot find it… I wonder if you might have some ideas. The post and get request codes in Java are shown here:

public static String post(String url, Map<String, Object> params){
		HttpClient client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		post.addHeader("Content-Type","application/json");
		//set parameters
      List<NameValuePair> list = new ArrayList<NameValuePair>();  
      for(Entry<String, Object> e : params.entrySet()){
    	    list.add(new BasicNameValuePair(e.getKey(),String.valueOf(e.getValue())));
      	}
      try {
	      if(list.size() > 0){  
	          UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"UTF-8");  
	          post.setEntity(entity);  
	        }
	      HttpResponse response = client.execute(post);
	      HttpEntity entity = response.getEntity();
			StatusLine statusLine = response.getStatusLine();
			if(statusLine.getStatusCode() == HttpStatus.SC_OK){
				String content = EntityUtils.toString(entity, "UTF-8");
				if(content == null){
					content = "";
				}
				return content;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "";
}
public static String get(String url){
		HttpClient client = HttpClients.createDefault();

		HttpGet get = new HttpGet(url);
		get.addHeader("Content-Type","application/json");
		try {
			HttpResponse response = client.execute(get);
			HttpEntity entity = response.getEntity();
			StatusLine statusLine = response.getStatusLine();
			if(statusLine.getStatusCode() == HttpStatus.SC_OK){
				String content = EntityUtils.toString(entity, "UTF-8");
				if(content == null){
					content = "";
				}
				return content;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
}

Thanks!

1 Like

I have found the reason.

For the post request in Java, I should use StringEntity to set the entity instead of EntityUtils. Or the entity is not able to be detected by rasa. Also, I need to set the header of the get & post requests.

Thanks a lot for the help from you all! :smile:

you can simply use ReqBin and send requests to the server right from your browser. it’s easy and free