4

I have an API that is sending me a POST request (JSON) for testing. I was doing a lot with the JSON, but all of a sudden it stopped working and giving me a JSONDecodeError. I tried all sorts of things, like using request.POST but nothing worked correctly like I said this was working at one point. Any assistance is appreciated.

Test that gives the error: In the Windows Command prompt, running:

curl -X POST http://127.0.0.1:8000/webhook/webhook_receiver/ -d '{"foo": "bar"}'

Error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

View:

def webhook_receiver(request, *args, **kwargs):
    if request.method == 'POST':
        # Get the incoming JSON Data
        data = request.body.decode('utf-8')
        received_json_data = json.loads(data)
        return HttpResponse(received_json_data)
    else:
        return HttpResponse("not Post")
Zev
  • 3,423
  • 1
  • 20
  • 41
Jiroscopes
  • 545
  • 9
  • 25

1 Answers1

2

The culprit is the combination of your command quoting syntax and the Windows terminal interpreter (what you posted would have been fine using Bash for example).

See Escaping curl command in Windows for details.


The actual error (which you really ought to have posted) looks like this:

Exception Type: JSONDecodeError at /webhook/webhook_receiver/
Exception Value: Expecting value: line 1 column 1 (char 0)

That says the data you are passing into the decoder does not start with a valid character (such as a "{" if the JSON is supposed to be a dict, or "[" for an array). You can probably work out what the problem is really easily by adding a print() for the start of the data, e.g. like:

print('first few characters=<{}>'.format(data[:4]))
Zev
  • 3,423
  • 1
  • 20
  • 41
Shaheed Haque
  • 644
  • 5
  • 14
  • I was trying curl -X POST http://127.0.0.1:8000 -d '{"Foo":"bar"}' is that not correct? – Jiroscopes Jun 01 '18 at 23:46
  • It looks OK, but there is no substitute for looking at what the Python code is actually getting. After all, you are getting an error! – Shaheed Haque Jun 01 '18 at 23:50
  • I am not sure where to use this print statement. Do I put it in my views? – Jiroscopes Jun 02 '18 at 01:48
  • Yes, and you'll see the result in the terminal where you ran the server. I just tested your code and it worked for me (I didn't get an error) so the issue may be in something you haven't put in the question yet. – Zev Jun 02 '18 at 03:28
  • Ok, I get `first few characters=<'{fo>` which is valid is it not? Seems like maybe the double quotes are being stripped? – Jiroscopes Jun 02 '18 at 03:31
  • @Zev I tested it in a brand new project, app, venv and all, still same problem. – Jiroscopes Jun 02 '18 at 03:51
  • What terminal are you using to run curl? – Zev Jun 02 '18 at 03:53
  • Yes, you need to replace it with encoded quotes. See https://stackoverflow.com/questions/18612248/how-to-escape-single-quotes-into-double-quotes-into-single-quotes/18612754#18612754 – Zev Jun 02 '18 at 04:03
  • You may also want to try `curl -X POST http://127.0.0.1:8000/fire/webhook_receiver/ -H 'Content-type:application/json' -d '{"foo": "bar"}'` but I'm not on windows so I can't test it. – Zev Jun 02 '18 at 04:22
  • The culprit is the combination of your command quoting syntax and the Windows terminal interpreter (what you posted would have been fine using Bash for example) as per @zev. You don't need the content-type header as its presence merely tells the receiver how to interpret the body data...and at least in your closed world example, you already know it to be JSON. – Shaheed Haque Jun 02 '18 at 04:34