简体   繁体   English

如何在pythonflask(restplus)中的GET请求中将URL作为参数传递?

[英]How to pass URLs as parameters in a GET request within python flask (restplus)?

I am creating a REST API using python flask.我正在使用 python flask 创建一个 REST API。 The API is ready and works on port number 8000 of my localhost. API 已准备就绪,可在我的本地主机的端口号 8000 上运行。 Now I intend to give this REST API a user friendly interface for which I decided to go with python - restplus.现在我打算给这个 REST API 一个用户友好的界面,我决定使用 python - restplus。 I thought of calling this service (running on 8000) internally from swagger application running on 5000我想从 5000 上运行的 swagger 应用程序内部调用此服务(在 8000 上运行)

I was able to create the basic structure of the API (Swagger).我能够创建 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)

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.整个应用程序在端口 5000 上无缝运行(使用随机字符串作为参数)。但是当我传递的 URL 是实际链接时,应用程序返回 404 - 未找到的响应。 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?有没有办法特别处理 URL?

Should I encode them before sending a request.我应该在发送请求之前对它们进行编码吗? (This will make my parameters look ugly). (这会使我的参数看起来很难看)。 Is there something I am missing?有什么我想念的吗?

it seems that :看起来 :

@api.route('/HybridComparator/<path:url2>/<path:url1>')

should fix it ,it fixes the 404 but i am getting only "http:/" part of the param应该修复它,它修复了 404,但我只得到参数的“http:/”部分

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;但是对于新搜索者来说,这可能会派上用场;

replace <string:url2>/<string:url1> with <path:url2>/<path:url1><string:url2>/<string:url1>替换为<path:url2>/<path:url1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM