[Solved]-How to access get request data in django rest framework

18πŸ‘

βœ…

If you are using class based views :

POST provides data in request.data and GET in request.query_params

If you are using function based views:

request.data will do the work for both methods.

axios does not support sending params as body with get method , it will append params in url. so if you are using axios you will have to use query_params

Axios example code:

axios.get(API_URL, {
            params: {
                testData: 'test data',
                pageNum: 1
            }
        })
        .then(res => {
            console.log(res);
        })
        .catch(err => {
            console.log(err.response.data);
        });

DRF example code:

Class TestView(APIView):
    def get(self, request):
        test_data_var = request.query_params['testData']
        page_num_var = request.query_params['pageNum']

Note:
If you’re testing it in postman then put the get request query params in Params tab.

πŸ‘€Naveen Jain

0πŸ‘

For me the accepted answer did not work.

A much simplier solution I saw no one mention is the following :

Append them to the URL like this : yourApi.com/subdomain/?parameter1=something

  • Axios.js
const target = "someApiYouWantToFetch.com"
let parameter = "DataYouWantToSend"
axios.get(`${target}?parameter=${parameter }`)
  • views.py
def get(self,request,*args,**kwargs): #or def list()
    data = request.GET.get('name')

0πŸ‘

In DRF if you want to access request.GET you should use request.request.GET

for example

@action(methods=['GET',], detail=False)
def activation(request, *args, **kwargs):
    uid = request.request.GET.get('uid')
    token = request.request.GET.get('token')
πŸ‘€Bambier

Leave a comment