pkm
pkm

Reputation: 2783

POST list to django restapi

I am trying to post a list of strings with some data but was unable to receive the list on server it gave me only last element of list

con = ["71qweq74520", "8324wqe57", "81ewqewq166"] 

received

con = 81ewqewq166

code to receive at server API:-

@csrf_exempt
@api_view(['POST'])
def getUser(request):
    if request.method == 'POST':
        if isapiValid(request):
            for params in request.POST:
                print params,request.POST[params]
                #this prints last element of array/lis

python scirpt to post:-

con = ["71qweq74520", "8324wqe57", "81ewqewq166"] 
data = { 'apikey':apikey, 'sig':sig ,'con': con}
data2 = json.dumps(data)
#hostname = '127.0.0.1:8000'
hostname = 'XX.XX.XX.XX'
method = 'method'
task = 'getUser'
url = 'http://'+ hostname + '/' + method + task
r = requests.post(url, data=data)
#r = requests.post(url, data=data2) this too fails

How can i successfully get the posted list ??

Output from Httpd log:-

 [Wed Jan 27 13:35:05.868468 2016] [:error] [pid 18858] API validation passed
    [Wed Jan 27 13:35:05.868512 2016] [:error] [pid 18858] 81ewqewq166
    [Wed Jan 27 13:35:05.868547 2016] [:error] [pid 18858] one 99qwerty99
    [Wed Jan 27 13:35:05.868566 2016] [:error] [pid 18858] apikey 4618d76f2fb84eacbac3339e5c7f2b57
    [Wed Jan 27 13:35:05.868589 2016] [:error] [pid 18858] sig e8fe50c733ec6513c91f10caf63e7864
    [Wed Jan 27 13:35:05.868608 2016] [:error] [pid 18858] con  81ew
qewq166

Things recceived in request.post is :-

 {
            "_content_type": "application/json",
            "_content": "{\"one\": \"9998889999\",\"con\": [7106174520, 8324100257]}\r\n"
        }

But when i do request.POST.get('con') only last element is received ???

Upvotes: 3

Views: 2065

Answers (1)

Kacper Dziubek
Kacper Dziubek

Reputation: 1693

This is one of the funny things in Django. If you want to get a list from a post request you should use request.POST.getlist(your_key). In your case request.POST.getlist('con').

You can find more about this topic here.

Upvotes: 7

Related Questions