simplejson.errors.JSONDecodeError

Я пытаюсь вызвать API от почтальона, но у меня появляется ошибка в консоли. В приведенном ниже коде я пытаюсь декодировать его и обрабатывать в соответствии с ним.

API - https: // localhost: 5005 / abc / xyz
Метод - ПОЧТА

Данные - {"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:Anaconda3libsite-packages	wistedwebserver.py", line 258, in render
            body = resrc.render(self)
          File "C:Anaconda3libsite-packagesklein
esource.py", line 210, in render
            d = defer.maybeDeferred(_execute)
          File "C:Anaconda3libsite-packages	wistedinternetdefer.py", line 151, in maybeDeferred
            result = f(*args, **kw)
          File "C:Anaconda3libsite-packagesklein
esource.py", line 204, in _execute
            **kwargs)
        --- <exception caught here> ---
          File "C:Anaconda3libsite-packages	wistedinternetdefer.py", line 151, in maybeDeferred
            result = f(*args, **kw)
          File "C:Anaconda3libsite-packageskleinapp.py", line 128, in execute_endpoint
            return endpoint_f(self._instance, *args, **kwargs)
          File "C:Anaconda3libsite-packageskleinapp.py", line 227, in _f
            return _call(instance, f, request, *a, **kw)
          File "C:Anaconda3libsite-packageskleinapp.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:Anaconda3libsite-packagesflaskjson\__init__.py", line 205, in loads
            return _json.loads(s, **kwargs)
          File "C:Anaconda3libsite-packagessimplejson\__init__.py", line 535, in loads
            return cls(encoding=encoding, **kw).decode(s)
          File "C:Anaconda3libsite-packagessimplejsondecoder.py", line 370, in decode
            obj, end = self.raw_decode(s)
          File "C:Anaconda3libsite-packagessimplejsondecoder.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',
    },

})

Всего 1 ответ


Вы можете получить контент напрямую в json с помощью request.get_json()