Я пытаюсь вызвать API от почтальона, но я получаю сообщение об ошибке в своей консоли.В приведенном ниже коде я пытаюсь расшифровать его и обработать в соответствии с ним.
API- https://localhost:5005/abc/xyz
Method-POST
Данные - {"q": "hi"}
server.py
def request_parameters(request):
if request.method.decode('utf-8', 'strict') == 'GET':
return {
key.decode('utf-8', 'strict'): value[0].decode('utf-8',
'strict')
for key, value in request.args.items()}
else:
content = request.content.read()
try:
return json.loads(content.decode('utf-8', 'strict'))
except ValueError as e:
logger.error("Failed to decode json during respond request. "
"Error: {}. Request content: "
"'{}'".format(e, content))
raise
Полная трассировка стека
Failed to decode json during respond request. Error: Expecting value: line 1 column 1 (char 0). Request content: 'b'''
2019-05-14 18:21:53+0530 [-] Unhandled Error
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\twisted\web\server.py", line 258, in render
body = resrc.render(self)
File "C:\Anaconda3\lib\site-packages\klein\resource.py", line 210, in render
d = defer.maybeDeferred(_execute)
File "C:\Anaconda3\lib\site-packages\twisted\internet\defer.py", line 151, in maybeDeferred
result = f(*args, **kw)
File "C:\Anaconda3\lib\site-packages\klein\resource.py", line 204, in _execute
**kwargs)
--- <exception caught here> ---
File "C:\Anaconda3\lib\site-packages\twisted\internet\defer.py", line 151, in maybeDeferred
result = f(*args, **kw)
File "C:\Anaconda3\lib\site-packages\klein\app.py", line 128, in execute_endpoint
return endpoint_f(self._instance, *args, **kwargs)
File "C:\Anaconda3\lib\site-packages\klein\app.py", line 227, in _f
return _call(instance, f, request, *a, **kw)
File "C:\Anaconda3\lib\site-packages\klein\app.py", line 50, in _call
result = f(*args, **kwargs)
File "server.py", line 61, in parse
request_params = request_parameters(request)
File "server.py", line 22, in request_parameters
return json.loads(content.decode('utf-8', 'strict'))
File "C:\Anaconda3\lib\site-packages\flask\json\__init__.py", line 205, in loads
return _json.loads(s, **kwargs)
File "C:\Anaconda3\lib\site-packages\simplejson\__init__.py", line 535, in loads
return cls(encoding=encoding, **kw).decode(s)
File "C:\Anaconda3\lib\site-packages\simplejson\decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "C:\Anaconda3\lib\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)
script.js
function respond(msg) {
data = {
query: msg //msg is getting from chatbot
}
fetch(`${url}/conversations/default/respond`, {
mode: 'no-cors',
method: 'POST',
// dataType:'jsonp',
q: data,
headers: {
'Content-Type': 'application/json',
},
})