简体   繁体   中英

Initiating a GET request to Django web api and send a list of ids as an array

I'm trying to call a rest API in django and send a list of Ids as a paremeter (I can't use query string).

This works if I send only one, but how to specify it for an array of integers:

path('details/<int:id>/', views.get_details),

def get_details(request, id=0)

What if I have a method that will take a list of Ids:

def get_data(request, ids=[])

How should the url be defined so that the ids array is populated?

You can parse ids list manually like this


path('details/<ids>/', views.get_details),

def parse_ids(ids: str):
    result = []
    for id in ids.split(','):
        try:
            result.append(int(id))
        except ValueError:
            pass
    return result

def get_details(request, ids):
    id_list = parse_ids(ids)
    # do stuff
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM