2

Here's the full traceback of the error I am getting:

ERROR 2022-11-08 13:29:54,926: Internal Server Error: /voice_chat/rooms
Traceback (most recent call last):
  File "C:\Users\15512\anaconda3\lib\site-packages\asgiref\sync.py", line 451, in thread_handler
    raise exc_info[1]
  File "C:\Users\15512\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 38, in inner
    response = await get_response(request)
  File "C:\Users\15512\anaconda3\lib\site-packages\django\core\handlers\base.py", line 233, in _get_response_async
    response = await wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\15512\anaconda3\lib\site-packages\asgiref\sync.py", line 414, in __call__
    ret = await asyncio.wait_for(future, timeout=None)
  File "C:\Users\15512\anaconda3\lib\asyncio\tasks.py", line 455, in wait_for
    return await fut
  File "C:\Users\15512\anaconda3\lib\concurrent\futures\thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\15512\anaconda3\lib\site-packages\asgiref\sync.py", line 455, in thread_handler
    return func(*args, **kwargs)
  File "C:\Users\15512\anaconda3\lib\site-packages\django\views\generic\base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
    request_body = json.loads(decode_request)
  File "C:\Users\15512\anaconda3\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Users\15512\anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\15512\anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ERROR 2022-11-08 13:29:54,959: HTTP POST /voice_chat/rooms 500 [0.32, 127.0.0.1:54258]

This is my code:

def post(self, request, *args, **kwargs):
    decode_request = request.body.decode("utf-8")
    print('decoded request body', decode_request)
    request_body = json.loads(decode_request)
    # print('request body', request_body)
    room_name = request_body.get("roomName")
    participant_label = request_body["participantLabel"]
    # print('username', curr_username)
    response = VoiceResponse()
    dial = Dial()
    dial.conference(
        name=room_name,
        participant_label=participant_label,
        start_conference_on_enter=True,
    )
    response.append(dial)
    return HttpResponse(response.to_xml(), content_type="text/xml")

Here is a sample of what I'm posting:

{"roomName":"testingUseremmanuelS21","participantLabel":"emmanuelS21","matchedUser":"testingUser"}

This is what I have in decode_request

AccountSid=***&ApiVersion=2010-04-01&ApplicationSid=***&CallSid=CAf4e
***&CallStatus=ringing&Called=&Caller=client%3AemmanuelS21&Direction=inbound&From=client%3AemmanuelS21&To=&participantLabel=emmanuelS21&roomName=testingUseremmanuelS21
Emm
  • 2,367
  • 3
  • 24
  • 50
  • `decode_request` is not in json format and/or is empty, therefore `json.loads(decode_request)` throws an error. Show us the contents of that variable. – John Gordon Nov 08 '22 at 19:42
  • @JohnGordon it definitely isn't empty, have added the contents of that variable to my question – Emm Nov 08 '22 at 19:56
  • 2
    That isn't in json format. Why are you trying to call `json.loads()` on it? – John Gordon Nov 08 '22 at 19:58
  • @JohnGordon It's sent to the backend as a JavaScript object and this is what I initially had with no errors (minus decode). Additionally, following this answer regarding how to parse 'request.body' https://stackoverflow.com/questions/29780060/trying-to-parse-request-body-from-post-in-django – Emm Nov 08 '22 at 20:02
  • @JohnGordon I meant this: https://stackoverflow.com/a/29781023/7158458 – Emm Nov 08 '22 at 20:07

0 Answers0