如何在python flask(restplus)中的GET请求中将URL作为参数传递? [英] How to pass URLs as parameters in a GET request within python flask (restplus)?
问题描述
我正在使用python flask创建REST API. API已准备就绪,并且可以在我的本地主机的端口号8000上使用.现在,我打算为该REST API提供一个用户友好的界面,为此我决定使用python-restplus.我想到了要从运行在5000上的swagger应用程序内部调用此服务(在8000上运行)
I am creating a REST API using python flask. The API is ready and works on port number 8000 of my localhost. Now I intend to give this REST API a user friendly interface for which I decided to go with python - restplus. I thought of calling this service (running on 8000) internally from swagger application running on 5000
我能够创建API(Swagger)的基本结构.其代码如下所示:
I was able to create the basic structure of the API (Swagger). The code for which looks like this:
import flask
from flask import Flask, request
from flask_restplus import Resource, Api
app = Flask(__name__)
api = Api(app)
@api.route('/HybridComparator/<string:url2>/<string:url1>')
class HybridComparator(Resource):
def get(self, url1, url2):
print(url1)
print(url2)
return url1 + ' ' + url2
if __name__ == '__main__':
app.run(debug=True)
整个应用程序在端口5000上无缝运行(使用随机字符串作为参数).但是,当我传递的URL是实际链接时,该应用程序将返回404-未找到.经过进一步的调查,我发现罪魁祸首被嵌入到我尝试提供的链接中的"/"中.有没有一种特别的方法来处理URL?
The application as a whole runs seamlessly (with random strings as parameters) on port 5000. But when the URLs I pass are actual links, the application returns a response of 404 - Not found. Further to my investigation I realized the culprit being '/' embedded within the links I try to provide. Is there a way to handle URLs in particular?
我应该在发送请求之前对它们进行编码. (这会使我的参数看起来很难看).有什么我想念的吗?
Should I encode them before sending a request. (This will make my parameters look ugly). Is there something I am missing?
推荐答案
这是一个完全古老的问题,我相信您现在已经解决了您的问题. 但是对于新的搜索者来说,这可能会派上用场;
This is an entirely old question and I am sure you solved your problem by now. But for new searchers, this may come in handy;
将<string:url2>/<string:url1>
替换为<path:url2>/<path:url1>
这篇关于如何在python flask(restplus)中的GET请求中将URL作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!