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

☆樱花仙子☆ 提交于 2020-06-27 18:48:10

问题


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

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)

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?


回答1:


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




回答2:


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>



来源:https://stackoverflow.com/questions/44236075/how-to-pass-urls-as-parameters-in-a-get-request-within-python-flask-restplus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!