2

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?

Harry
  • 137
  • 1
  • 7
  • wont this be a problem if your parameters contain slashes? – Nullman May 29 '17 at 06:50
  • Yes, I am facing that problem. Any known solutions ? – Harry May 29 '17 at 06:52
  • unfortunatly, im not aware of anything other than encoding – Nullman May 29 '17 at 06:53
  • does [this](http://flask.pocoo.org/snippets/76/) help? – Nullman May 29 '17 at 06:57
  • Encoding the URLs and providing them as parameters does the job. I simply have to decode them in the application using urllib. I was keen on getting a solution which does not need me to provide encoded URLs – Harry May 29 '17 at 06:57
  • @Nullman, I tried defining the parameter as 'path' data type instead of traditional strings. It works. But gives some weird results. I am trying to solve it using the same. Will update you as and when I have something. Thanks a bunch! – Harry May 29 '17 at 07:02

2 Answers2

1

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>

Conrad747
  • 45
  • 7
0

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