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")
2
  • Is everything running? What data is being sent? Commented Jun 1, 2018 at 23:01
  • @Zev curl -X POST 127.0.0.1:8000/webhook/webhook_receiver -d '{"foo": "bar"}' is what I was trying Commented Jun 2, 2018 at 1:56

1 Answer 1

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]))
Sign up to request clarification or add additional context in comments.

10 Comments

I was trying curl -X POST 127.0.0.1:8000 -d '{"Foo":"bar"}' is that not correct?
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!
I am not sure where to use this print statement. Do I put it in my views?
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.
Ok, I get first few characters=<'{fo> which is valid is it not? Seems like maybe the double quotes are being stripped?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.