0

I'm trying to write a get request for a list of actors using flask. Here is an example json data object:

{
 "age": 61, 
 "json_class": "Actor",  
 "name": "Bruce Willis", 
 "total_gross": 562709189
}

How would I write the flask get request such that '/actors?attr={attr_value}' would filter out such that if it was "actors?name='Bruce'", would give me all actors with the name Bruce.

Something along this line I believe:

@app.route('/actors? (what here)', methods=['GET'])
def actorByAttr(name):
   names = [data[string] for string in data if data[string]['json_class'] == 'Actor' and string == name]
   if len(names) > 0:
     return jsonify(names[0])
   else:
     return jsonify({'message': "No one found with name: "+ name})

EDIT: Also, how could I incorporate AND and OR operators, such as:

name=”Bruce”|name=”Matt”, name=”Bruce”&age=61 
hawk2015
  • 9
  • 2
  • Possible duplicate of [Python Flask how to get parameters from a URL?](http://stackoverflow.com/questions/24892035/python-flask-how-to-get-parameters-from-a-url) – FuzzyAmi Mar 05 '17 at 12:42
  • what you're asking about is called URL Parameters. have a look at http://stackoverflow.com/questions/24892035/python-flask-how-to-get-parameters-from-a-url for example on how to parse url parameters from a GET request – FuzzyAmi Mar 05 '17 at 12:43
  • @FuzzyAmi Thanks, that go me everything except the OR operator. When I AND, it parses it fine, but OR gives this: ImmutableMultiDict([('age', u'28|age=61')]) ValueError: invalid literal for int() with base 10: '28|age=61' – hawk2015 Mar 05 '17 at 18:27
  • 1
    yes, there's a trick here: the "&" *is* part of the URL params spec, but "|" is *not*. and actually, "&" doesn't mean "and" in this context. its means, "the next parameter". when you request with multiple parameters, the "&" delimits one parameter from another: param1=value1&param2=value2 etc. to correctly handle "|" you'll need to parse the entire param *as one string*: "Tom|Jerry" and then use string manipulation to split the string at the "|" char, and get the names. – FuzzyAmi Mar 06 '17 at 06:28

0 Answers0