Home > OS >  Django - Passing a json or an array in URL for an API call
Django - Passing a json or an array in URL for an API call

Time:10-22

I want to pass a number off variables (either as a JSON or Array) via an API, such as:

{'age': 35, 'gender':'female', ...etc}

I am not sure how to pass this information into the Djano URL. I could set up individual parameters in the URL, but I got quite a few to pass. there must be an easier way of doing it

SOLUTION: Solved by switching of a POST Call in the API and setting up the serializers for each variable so that they can be passed through the request.

CodePudding user response:

I am using axios to make calls from django backend. In my case I can do:

js.file:

function search(token, status, q) {
    return (dispatch) => {
        dispatch(start(token));
        axios
            .get(`${serverIP}/invoices/sales-invoice/`, {
                params: {
                    status: status,
                    q: q,
                },
            })
            .then((res) => {
                dispatch(successSearch(res.data, status));
            })
            .catch((err) => {
                dispatch(fail(err));
            });
    };
}

Here I am sending 2 params, but you can actually send object, for example user info. And than in views get them

views.py:

def list(self, request, *args, **kwargs):
        status = request.query_params.get('status')
        q= request.query_params.get('q')

These is exapmple with DRF model viewset

  • Related