I'm trying to write a quick api that will search through a json object and return the current account if the iban is the same. If it doesn't find any iban it should return a 404.
This is what I have so far:
from flask import Flask
from flask import request
from flask import abort
import json
app = Flask(__name__)
accounts = {
"accounts": {
"account": {
"naam": "Mark Roording",
"iban": "NL93RABO4923460458",
"straat": "Pluviusstraat",
"straatnummer": 5,
"postcode": "7321EL",
"plaats": "Apeldoorn"
},
"account": {
"naam": "Carleen van der Snoek",
"iban": "NL80INGB7619798757",
"straat": "Franciscanenstraat",
"straatnummer": 160,
"postcode": "1566LC",
"plaats": "Assendelft"
},
"account": {
"naam": "Maria Lingen",
"iban": "NL28INGB6291533782",
"straat": "Weterschoten",
"straatnummer": 135,
"postcode": "7381AL",
"plaats": "Klarenbeek"
},
"account": {
"naam": "Betty Kelder",
"iban": "NL70INGB6143537119",
"straat": "Utrechtseweg",
"straatnummer": 111,
"postcode": "6862AC",
"plaats": "Oosterbeek"
},
"account": {
"naam": "Timmie Ruijgrok",
"iban": "NL41ABNA9079565997",
"straat": "Kuipersstraat",
"straatnummer": 27,
"postcode": "1074EK",
"plaats": "Amsterdam"
}
}
}
@app.route('/get_accountdetails')
def get_accountdetails():
paramiban = request.args.get('iban', type=str)
for k, acctinfo in accounts['accounts'].items():
if acctinfo['iban'] == paramiban:
response = app.response_class(
response=json.dumps(acctinfo),
status=200,
mimetype='application/json'
)
return response
else:
return abort(404)
So if iban is NL28INGB6291533782 it would return the current account and a 200 so like this:
{"naam": "Maria Lingen", "iban": "NL28INGB6291533782", "straat": "Weterschoten", "straatnummer": 135, "postcode": "7381AL", "plaats": "Klarenbeek"}
EDIT: so I changed the code and if I type in the last iban it works and returns the value. If it's any other iban it doesn't work.
from flask import json
@app.route('/get_accountdetails')
def get_accountdetails():
iban = request.args.get('iban')
for dict in accounts:
if dict['iban'] == iban:
response = app.response_class(
response=json.dumps(dict['account']),
status=200,
mimetype='application/json'
)
return response
else:
return abort(404)
it will help to send json response with 200 status code.
You process accounts, while you should process accounts["accounts"]:
iban = 'NL41ABNA9079565997'
for k, acctinfo in accounts['accounts'].items():
if acctinfo['iban'] == iban:
print(acctinfo)
prints:
{
"naam": "Timmie Ruijgrok",
"iban": "NL41ABNA9079565997",
"straat": "Kuipersstraat",
"straatnummer": 27,
"postcode": "1074EK",
"plaats": "Amsterdam"
}
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.