2

I have a simple rest api which I want to send a post request to using requests.

My url pattern is this:

url(r'^$', views.ProductList.as_view())

Inside my view I have:

class ProductList(generics.ListAPIView):
    serializer_class = ProductSerializer

    def post(self, request, format=None):
        print('THIS IS A POST REQUEST')
        queryset = [product.name for product in Product.objects.all()]
        return Response(queryset)

And I am trying to send a post request using:

response = requests.post('http://127.0.0.1:8080/')

However this returns a 403, and the print statement isn't printed. I have done some research and I think it may have something to do with the CSRF token not being there but i'm not to sure how to add that. Does anybody know how I can get the post request to work?

I'm using python 3.6.3 and Django 1.10

NoisyB
  • 65
  • 2
  • 8

1 Answers1

1

ListAPIView is meant for only listing your products hence the POST requests are forbidden.

ListAPIView

Used for read-only endpoints to represent a collection of model instances.

taken from Django Rest Framework documentation

You should use ListCreateAPIView:

class ProductList(generics.ListCreateAPIView):
    """
    List all products or create a product.
    """
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
Joshua Chan
  • 1,797
  • 8
  • 16
  • I'm still getting the 403. If I use this what permissions should I have for it. I don't really need any permissions in general on my site, so anything which would allow the post request to be sent to the API. – NoisyB May 20 '18 at 07:30
  • Where exactly are you sending the post request from? Are you still using your self-defined post function? Why are you using `queryset = [product.name for product in Product.objects.all()]` for a post request? If you want to create a product in your backend, simply use ListCreateAPIView and send the necessary data according to your serializer using the class above. – Joshua Chan May 20 '18 at 08:11
  • im sending the post request using python requests. I understand what you are saying about the queryset variable, but I'm still confused about how I would go about adding to the database then by using ListCreateAPIView – NoisyB May 20 '18 at 08:16
  • is the python request from another server? Why do you need an API if not. You need to send a post request with form data that contains the json representation of your ProductSerializer (should be a ModelSerializer else you need to manually create the object when receiving the data) – Joshua Chan May 20 '18 at 08:24
  • right now its all from the same computer, however once it's finished I intend to put it on a server which is why I need the API. I am sending it a post request using request.post(the URL, json={data here}) this command is just running through a python shell on my computer – NoisyB May 20 '18 at 08:30
  • your parameter seems to be wrong, should be data instead of json. See https://stackoverflow.com/questions/4476373/simple-url-get-post-function-in-python for examples on how to send json data with post – Joshua Chan May 20 '18 at 08:34
  • im using json and that seems to work fine. As some people have suggested in the comments I have added a csrf exempt and changed the permissions to AllowAny which gets rid of the 403 and he post request works fine. But apparently I shouldnt, or is bad practice to put AllowAny in the permissions – NoisyB May 20 '18 at 08:38