djangohttppostman

Can't get Information out of request data


I'm trying to build an API with Django. I think I build it correctly but from, for example Post-request data, I get as the field values "none" Maybe you can help me to solve this problem :)

Here's the API:

def apiRequests(request):
    if request.method == "POST":
        print(request.POST.get("name"))
        print(request.POST.get("publisher"))
        print(request.POST.get("price"))
        #Game.objects.create(name= request.POST.get('name'),publisher = request.POST.get("publisher"),price = request.POST.get("price"))
    elif request.method == "GET":
        response = Game.objects.filter(name = request.GET.get("name"))
    return render(request,"apirequests.html")

As you may see I'm printing out the data I receive. This looks like this:

None
None
None
[26/Aug/2022 08:40:09] "POST /api/ HTTP/1.1" 200 263

Here's the model class:

class Game(models.Model):
    name = models.CharField(max_length=200)
    publisher = models.CharField(max_length=200)
    price = models.BigIntegerField()

    def __str__(self):
        return str(self.id) + " " + self.name

And here is the data I'm sending as a Post-request from Postman to the API:

{
    "name": "FarCry",
    "publisher": "EA",
    "price": "35.66"
}

I think I should say that I got problems with the CsrfViewMiddleware-Token so I commented it out in the settings.py, maybe there is the problem?


Solution

  • If you are using Postman, send the body as form-data.