Working on Flask to get List of int and return those values as endpoint

8k views Asked by At

I want to use Flask in such a way where I can simply pass use localhost/data/?user=[123,234,345] and it will return those values in the body.

This is what I've tried:

import redis 
r = redis.StrictRedis(host='localhost', port=6379, db=0)
@app.route('/data', methods=['GET','POST']) 
def data():
    user = request.args.getList('user', type=int)
    users = []

    for u in user:
        try:
            users.append(redisfunction(u)) 
        except:
            continue

    return users

What am I doing wrong?

1

There are 1 answers

4
superK On BEST ANSWER
  1. request.args.getList should be request.args.getlist, lowercase of l, or it will cause error code 500, the reason I'm not find now
  2. you should visit localhost/data?user=123&user=234&user=345

here is my simpe code:

from flask import Flask, request

app = Flask(__name__)

@app.route('/data', methods=['GET', 'POST'])
def data():
    user = request.args.getlist('user', type=int)
    print user
    return 'done'

app.run()

then curl 'localhost:5000/data?user=123&user=234&user=345'

flask output:

[123, 234, 345]

127.0.0.1 - - [14/Dec/2016 09:23:48] "GET /data?user=123&user=234&user=345 HTTP/