从url传递参数到方法时出错



我使用Flask作为我的webservice,当我尝试访问以下url

http://127.0.0.1:5000/2,3/5,6

我收到以下错误:

return self.view_functions[rule.endpoint](**req.view_args)
TypeError: getDistance() got an unexpected keyword argument 'startCoord'

如何修复这个错误?

:

@app.route("/<string:startCoord>/<string:endCoord>",methods=['GET'] )
def getDistance():
startLat, startLng = startCoord.split(',')
end1Lat, endLng = endCoord.split(',')
print("/api.openrouteservice.org/v2/directions/driving-car?api_key=" + config['EndPoint']['api_key'] + "&start=" + startLat + "," + startLng + "&end=" + end1Lat + "," + endLng)

:

['API_KEY']
<Section: API_KEY>

你应该这样定义你的路由:

@app.route("/<string:startCoord>/<string:endCoord>",methods=['GET'] )
def getDistance(startCoord, endCoord):
startLat, startLng = startCoord.split(',')
end1Lat, endLng = endCoord.split(',')

url编码的参数应该在函数参数中:

@app.route("/<string:startCoord>/<string:endCoord>",methods=['GET'] )
def getDistance(startCoord,endCoord):

https://flask.palletsprojects.com/en/1.1.x/quickstart/variable-rules

最新更新