I am using Azure Functions to get data from Azure SQL database, and tried to get value under three conditions. I will need to input three parameters to extract the rate value, so I will need to make my url dynamic with three paras.
In my init.py code, the route for getting the corresponding record:
@app.route("/forwardrate/<valuationDate>/<forwardDate>/<tenor>", methods=['GET'])
def get_forward_rate(valuationDate, forwardDate, tenor):
try:
query = ''
valuationdate = valuationDate[:2] '/' valuationDate[2:4] '/' valuationDate[4:]
forwarddate = forwardDate[:2] '/' forwardDate[2:4] '/' forwardDate[4:]
query = "SELECT rate FROM database where [valuationDate] = \'{valuationdate}\' AND [forwardDate] = \'{forwarddate}\' AND [tenor] = {tenor}".format(valuationdate = valuationdate, forwarddate = forwarddate, tenor = tenor)
db = get_db()
cursor = db.execute(query)
row = cursor.fetchone()
#logger.info(query)
if row is None:
rate = 'No Rate Found!'
else:
rate = str(row[0])
return rate
except:
logger.error(error_handling())
return '!ERROR'
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
return func.WsgiMiddleware(app).handle(req, context)
And in my function.json file, I set it as:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
],
"route": "/forwardrate/{valuationdate}/{forwarddate}/{tenor}"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
While I tested locally, I can successfully get data with input the paras directly in the url: http://localhost:7071/forwardrate/06302020/06302020/3
After I deployed to the cloud, under my function, I will get url (under Function > Overview): which will totally cause error, totally different from my running url, and couldn't get result with this url.
Is there anyway I can make the parameters be input as key-value in my Azure Functions with flask? Or how can I input the paras in postman to make it as my original format?
I am very new to the API things and also new to Azure platform, I highly appreciate all suggestions, Thanks!
CodePudding user response:
Have a look into Flask request.args
doc. Any URL parameters are made available in (essentially) a dict
. You'd then need to remove them from the URL path.
from flask import request
@app.route("/forwardrate")
def get_forward_rate():
valuationdate = request.args.get('valuationdate', '')
forwarddate = request.args.get('forwarddate', '')
tenor = request.args.get('tenor', '')
...
curl https://your.domain/forwardrate?valuationdate=1&forwarddate=2&tenor=3