.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8'

Hi all, I am getting the above-mentioned error when try CallbackInput method.

Rasa version is : Rasa 1.0.6

Here is my code,

credentials.yml

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

rasa:
    url: "http://localhost:5002/api"

callbackURL.py

Am calling this api using api client.

@app.route('/rasa_in',methods=['GET','POST'])
def wahtsapp_in():
wa_in = request.get_json(force=True)
wa_data_text = None
rasa_url = "http://localhost:5005/webhooks/callback/webhook"
for res in wa_in:
	if 'text' in res:
		wa_data_text = res.get('text').get('body')
	wa_data = {
		"sender":"Rasa",
    	"message":wa_data_text
	}
	headers = {
"Content-Type": "application/json"
		}

data=json.dumps(wa_data,sort_keys=True, indent=3)
r = requests.post(url=rasa_url, headers=headers, data=data)
print('r',r) **// Here it print  500 Error**
return r.content

Here is callback URL

 @app.route('/rasa_callback',methods=['GET','POST'])
def rasa_callback():
headers = {
  "Content-Type": "application/json"
	}
rasa_data = request.get_json(force=True)
api_url = 'https://my_url'
ts = time.time()
rasa_body = ''
if 'text' in rasa_data:
	rasa_body = rasa_data.get('text')
wa_data ={
			"text":{"message":rasa_body}
		}



r = requests.post(url=api_url,headers=headers, data=json.dumps(wa_data))
return r.content # I treid below return too, but no luck
return json.dumps(r.text,sort_keys=True, indent=3)

How can i resolve this?

Looking at reproducing this now, on your callbackURL.py I’m assuming from that snippet you are just using flask with routes right?

I’m going to try and make the same code to test it so just ensuring I’m right here looks like it though.

@btotharye, yes i am using flask with routes.

So this appears to be a issue with using request.get_json() from looking at other posts about it, it can cause a lot of errors.

I’d look into using maybe request.args or request.forms and try it from there. This doesn’t appear to be a Rasa related issue but more around this custom webhook code.

Example stack overflow post about this - json - Flask Bad Request 400 - Stack Overflow

Thanks

@btotharye, As you said I tried request.args with POST method. But I am getting None. Here is what I tried.

  request.form['text']
  request.form.get('text')
  request.form.to_dict()
  request.form.getlist('text[]')
  request.form.getlist('text')

I got None or {} as output for all the trials.

Same time i got a dictionary with values if I use get_json().

Here is data format:

   [{
	    "from": "971559xxxxxx",
	    "id": "ABEGkZiAOQVVxxxxxxxxxxxxqEZ7KVtbxxxxx",
	    "profile": {
	        "name": "John Doe"
	    },
	    "text": {
	        "body": "hi"
	    },
	    "timestamp": "1562080688",
	    "type": "text",
	    "to_number": "+971xxxxxxx"
		}
 ]

Thanks.

EDIT: I managed to get the data using request.json. But still i am getting the same error. One question, What should i return in callback funciton ? Because, when i return a string or any other valid response i am getting above mentiond error. If i return and wrong data like {} or 'request responsedirectly i am getting error like Invalid return type but same time in parent fucntion/URL/API client i am getting responsesuccess`.

So what should i return in call back funtion? Thanks

@kabeer I was facing same problem. I returned jsonify(success=True) and it worked for me.