3

I'm using postman to create a POST request to my django view

@csrf_exempt
@api_view(['POST'])
def create_user(request):
    data = JSONParser().parse(request)
    serializer = CaretakerSerializer(data=data)
    if serializer.is_valid():
        serializer.save()
        return JSONResponse(serializer.data, status=201)
    return JSONResponse(serializer.errors, status=400)

but I get the following error:

{
  "detail": "JSON parse error - No JSON object could be decoded"
}

When trying to print my request.body I get the following:

------WebKitFormBoundaryrg1JNLvBOEfjkQAT
Content-Disposition: form-data; name="name"

Rubencito
------WebKitFormBoundaryrg1JNLvBOEfjkQAT
Content-Disposition: form-data; name="email"

[email protected]
------WebKitFormBoundaryrg1JNLvBOEfjkQAT--

This is the postman screenshot:

enter image description here

1 Answer 1

5

Well you are not sending json, you are sending regular POST request with name/value pairs.

You need to switch postman to "raw" format and type something like {"name": "X", "email":"Y"}

Then on a python side you can read it as json.loads(request.body)

Sign up to request clarification or add additional context in comments.

1 Comment

How can we get form-data in django view?

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.