1

I do a request to my local server using postman like this:

POSTMan post request

As you can see it's a post request. In my view (APIView) I need access to the json data. But when I try:

request.POST # <QueryDict: {}> 

or

request.data # ¿? # AttributeError: 'WSGIRequest' object has no attribute 'data' 

The only way I can see the sent data is when I access to

request.body # '{\n "token": "6J3qG4Ji2Jw44eIklKvPYxUgclfGRWHZDKG",\n "city": "Port Orange",\n "state": "FL",\n "formatted_address": "Peach Blossom Blvd 5329",\n "_zip": "32128"\n}' 

But this is a 'str'

>>> type(request.body) <type 'str'> 

I an trying to access to the request's data in dispatch() method. I can do this:

req = self.initialize_request(request) 

This returns a rest_framework.request.Request object and I can access to request data. But then I can't call

super(FaveoAPIView, self).dispatch(request, *args, **kwargs) 

Because I get:

{ "status_code": 400, "object": "Malformed request.", "success": false } 

I can'r understand why, I guess that when I call self.initialize_request() something change. Any idea?

2
  • What is the actual problem you're trying to solve for here? Overriding the dispatch() method is, in most cases, not actually the best way to go about solving a problem. Commented Nov 16, 2015 at 22:20
  • @JoeyWilhelm I don't want to override dispatch, I want to execute some code before the normal View flow. But I need to access to request.data, before my post (or get) method executes. Commented Nov 16, 2015 at 22:25

2 Answers 2

2

I believe you should be able to override the initial() method on your view in order to achieve what you're attempting. The docstring from the APIView class reads:

""" Runs anything that needs to occur prior to calling the method handler. """ 

So, it would look something like:

class FaveoAPIView(APIView): def initial(self, request, *args, **kwargs): super(FaveoAPIView, self).initial(request, *args, **kwargs) # Manipulate request.data to your heart's content here 

In APIView, this is called immediately before your method handler.

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

1 Comment

Wihelm I can see this is executed during dispatch and before post or get, but this method is suposed to doesn't return anything, so I can't return a Response with an error message if my validations fails here. Or is it possible?
0

super(FaveoAPIView, self).dispatch(request, *args, **kwargs) will call initialize_request turning the request into a DRF one. This can only be once therefore it'll fail after that.

Instead of fixing a broken solution, could you rather tell us about your actual issue, i.e. why you need to access the requests data before the view itself is called ?

3 Comments

There're maybe another way to do this, but I was ising this to check some values in the POST data, to check its data type, or if they are a allowed value, for example, I need an integer, but lt 100 and gt 5. And I'd like apply this methods to all my views. If the request's data does not pass the conditions, I don't wat the POST or GET method to be executed.
This is what serializers are for.
I would have to write methods in every serializer, since serializers are executend inside post or get method. I want to avoid execute post or get methods if conditions fails.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.