I have the following code on my service and when requested the return is always 404.
#app.route('/v1/auth/service', methods=['POST'])
def verifyAuthService():
data = request.get_json()
But in the log file, the service returns 404.
127.0.0.1 - - [TIMEVALUE] "POST /v1/auth/service HTTP/1.1" 404 -
But it works when I use other route. I have checked if the route path or method name are duplicated and didn't find anything.
I request the service method with the following code:
r = requests.post("http://myservice.com:5001/v1/auth/service", json=jPayload)
Maybe was a newbie error, in my init.py file, I haven't imported auth_services.py.
The /v1/auth/service route wasn't interpreted by python so, the route was inaccessible.
Can you try building the URL with below code and match if the route is pointing to exactly same URL which you have called.
from flask import Flask, url_for
app = Flask(__name__)
#app.route('/v1/auth/service', methods=['POST'])
def verifyAuthService():
data = request.get_json()
with app.test_request_context():
print url_for('verifyAuthService')
Hope this helps!
Related
I want my web to be able to handle URLs inside the rule,
just like:
http://127.0.0.1:5000/tiyee?url=https://tiyee.cn/iyu2
but getting an error:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
I've tried with this code below but seems like it doesn't work
from flask import Flask, redirect
from tiyee import bypasser
app = Flask(__name__)
#app.route('/tiyee?url=<url>')
def _tiyee_redirect(url):
bypassed_json = bypasser(url)
return redirect(bypassed_json['bypassed_link'])
if __name__ == '__main__':
app.run(debug=True, port=5000)
Q: Is there a way to add URL inside the route rule?
example:
example.com/tiyee?url=https://tiyee.cn/iyu2 and get the https://tiyee.cn/iyu2
You are passing the url data in query. You will need to use request object to get the query value.
from flask import request
...
#app.route('/tiyee')
def _tiyee_redirect():
_url = request.args.get('url')
if _url is not None:
bypassed_json = bypasser(_url)
return redirect(bypassed_json['bypassed_link'])
I am using python and flask to create a web API. However I am getting trouble in requesting the HTTP status code of my localhost.
My code:
import requests
import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/home', methods=['GET'])
def home():
r = requests.get(url="http://localhost:5000/home")
print(r.status_code)
return "Welcome!"
app.run()
Before adding the line for requesting status code, it works fine in my browser (Chrome) and the command prompt show something like this:
127.0.0.1 - - [19/Sep/2019 01:03:54] "GET /home HTTP/1.1" 200 -
After adding the line for requesting, it keeps loading (forever) in my browser and no response in the command prompt.
I have no idea about this problem because it did not show any error and I have read some of the solutions mentioned in other similar problems (like disabling proxy) but it seems not working for me.
Thanks!
Look at it from the point of view of the app. It gets a request for /home and routes it to home(). While servicing that request, the app makes a request for /home, which routes to home(). During the servicing of that request... and so on until some resource is exhausted.
If you intent is to prove that you can make a request to the app from within the app, target a different endpoint.
I am following this tutorial initially I was trying to get a response using postman with the url
ec2-x-x-xxx-xx.eu-west-2.compute.amazonaws.com/:8080
but it would not return a response, so then I tried without the / at the end and it returned what I wanted, why is this happening as my flask route clearly has a / in it
My flask app looks like this
from flask import Flask
application = Flask(__name__)
#application.route("/")
def hello():
return "<h1 style='color:blue'>Hello There!</h1>"
if __name__ == '__main__':
application.run(host="0.0.0.0", port="8080")
The order of the parts of a URL is important.
The URL
ec2-x-x-xxx-xx.eu-west-2.compute.amazonaws.com/:8080
Is going to attempt port 80, and look for a path /:8080.
ec2-x-x-xxx-xx.eu-west-2.compute.amazonaws.com:8080/
Will attempt port 8080 and look for a path /
There are two different concepts that you are getting mixed up on here.
The line #application.route("/") defines the root of your site. That is the default entry point or path if you enter your site address in a browser without e.g /about at the end.
The address ec2-x-x-xxx-xx.eu-west-2.compute.amazonaws.com:8080 is a combination of the web server address and port, separated by a colon. You will not get a response if you alter this address. You could add a "/" after the 8080 to get to a particular page.
While testing the API functionality with curl, i tried sending a post data to the route below. while watching the debug, the views rather responded to a 301 redirect preventing to grab the needed data. what am i doing wrong?
here is my current views.
from flask import Flask, jsonify, render_template, request
from flask_cors import CORS
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route("/api/user/login/", methods=["GET", "POST"])
def login(*args, **kwargs):
print 'Got request for login'
print args
print kwargs
print request.args
print request.args.get("username")
print request.values.get("username")
print request.method
response = {'username': 'Erik'}
dict = request.args
for key in dict:
print 'form key ' + dict[key]
# return jsonify(response)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)
Calling the following code,
curl -d "username=Flash" http://0.0.0.0:8080/api/user/login
initiates a redirection
/home/user/fab/bin/python2.7 /home/user/PycharmProjects/myelm/server.py
* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
127.0.0.1 - - [06/Sep/2017 23:04:25] "POST /api/user/login HTTP/1.1" 301 -
Here is the documentation about this behavior:
Unique URLs / Redirection Behavior
Flask’s URL rules are based on Werkzeug’s routing module. The idea behind that module is to ensure beautiful and unique URLs based on precedents laid down by Apache and earlier HTTP servers.
Take these two rules:
#app.route('/projects/')
def projects():
return 'The project page'
#app.route('/about')
def about():
return 'The about page'
Though they look rather similar, they differ in their use of the trailing slash in the URL definition. In the first case, the canonical URL for the projects endpoint has a trailing slash. In that sense, it is similar to a folder on a filesystem. Accessing it without a trailing slash will cause Flask to redirect to the canonical URL with the trailing slash.
In the second case, however, the URL is defined without a trailing slash, rather like the pathname of a file on UNIX-like systems. Accessing the URL with a trailing slash will produce a 404 “Not Found” error.
This behavior allows relative URLs to continue working even if the trailing slash is omitted, consistent with how Apache and other servers work. Also, the URLs will stay unique, which helps search engines avoid indexing the same page twice.
Trying to use AJAX to POST to my Python script testing.py. Whenever I try the POST, I receive the following error.
POST http://localhost:5000/testing.py 404 (NOT FOUND)
I'm using Flask to serve up my website. Why is this a 404 error, and how do I get localhost to serve my python script?
somewhere you should have a file called app.py,(but you can call it testing.py if you want) inside should be at least:
from flask import Flask, request
app = Flask(__name__)
#app.route('/testing') # you can probably even put testing.py here
def testing():
vars = request.args
return ','.join(map(str,vars))
if __name__ == "__main__":
app.run()
then
python app.py # or testing.py
then you can send your POST to http://localhost:5000/testing
and it will print any posted parameters to the browser