I'm using DRF to build a RPC-style API where each endpoint can be called with either GET or POST method. So far I got the methods combined nicely:
class UpdateUser(APIView):
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, *args, **kwargs):
return self.get(request, *args, **kwargs)
def get(self, request, format=None):
# Do stuff here with request.data
Unfortunately POST provides data in request.data and GET in request.query_params. Is there a way to combine them both into request.data or something custom like request.params? Parsers doesn't seem to work as they aren't being called on GET requests.
I agree with @jorilallo' comments about using request.data inside get function.
Alternatively, what you could do is create another function in the view which can have either request.data or request.query_params as arguments:
class UpdateUser(APIView):
permission_classes = (permissions.IsAuthenticated,)
def post(self, request, *args, **kwargs):
# POST have request.data
return self.process_request(request, request.data)
def get(self, request, format=None):
# GET have request.query_params
return self.process_request(request, request.query_params)
def process_request(self, request, data):
# Do stuff here with data
# return a response
Here, process_request function is called from both post and get methods and relevant data is passed as arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With