简体   繁体   中英

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

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?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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