How to view single, trailing question mark in URL passed to Flask? - python

I'm writing a Flask application to simulate a piece of equipment that is accessed via HTTP. Queries to the equipment end in a ?; e.g. http://10.10.10.10/:MN? will return the model number.
I wrote the simulator to check the last character of the request.url. However, the question mark is not present in the request.
Test Code:
from flask import Flask, request
app = Flask(__name__)
#app.route("/<command>")
def handle_route(command):
resp = f"request.url: {request.url}"
return resp
if __name__ == "__main__":
app.run("127.0.0.1", 9023)
Checking http://127.0.0.1:9023/:MN? in a browser yields:
request.url: http://127.0.0.1:9023/:MN
Is the question mark in another Flask variable that I can access? I've checked request.url, request.root_url, request.base_url, request.path, and request.query_string and have not found the missing question mark.

Related

URL Encode String in Python Flask API?

I have made a REST API using Python-Flask, now the input for api_call is supposed to be a string and that string needs to have special characters included. The problem occurs here is that when I make the request with http://127.0.0.1:5000/api/<your string>, the browser redirected it as a Google Search Query and sometimes it just throw the Not Found 404 error and this only occurs when I use some special characters in my string like ?. Is there a way to encode the string before calling the api? What I have done is I have made an another program that urlencode the string passed and redirect it to my api call but that doesn't seem like the correct way to call api.
Here's my API code:
# Importing Libraries
from flask import Flask, jsonify, request
# Creating Flask App
app = Flask(__name__)
#app.route('/')
def index():
'''
index function, to inform user of the API route.
params: None
return: None
'''
return f"Visit:<br><strong>{request.url}api/(your string)</strong><br>to get the api response"
#app.route('/api/<string:body>')
def sample(body):
# the code for this function is different and can't be shared but the output works similar
return jsonify({'length': len(body)})
if __name__ == '__main__':
app.run(debug=True)
As I have mentioned the code for the sample() function is different from the original but the output generation is completely similar, the original code is also returning the dictionary which will then be jsonified for response just like the code here. The code for string_reformat is:
# Importing Libraries
from urllib.parse import quote
# URL Encoding String
input_string = input()
print(quote(input_string))

Why am I getting a response from the wrong API 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.

Post API-- what are the steps that need to be followed once the code is created so that I can add data into a txt file through this API

I am new to API, and get a tasks of creating POST API. I have created a code somehow.
I want to add data to the hello.txt through post API, So how will I do it?
Here is my code:
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
#app.route('/api/v1/resources/messages', methods = ['POST'])
def api_message():
if request.headers['Content-Type'] == 'text/plain':
return "Text Message: " + request.data
elif request.headers['Content-Type'] == 'application/octet-stream':
return "Binary message written!"
elif request.headers['Content-Type'] == 'application/json':
f = open('F:\Asif_Ahmed\Projects\api\hello.txt',"w")
f.write(request.data)
f.close()
return "JSON Message: " + json.dumps(request.json)
else:
return "415 Unsupported Media Type ;)"
app.run()
from flask import Flask, jsonify, render_template, request #import flask library
from flask_basicauth import BasicAuth # import flask library for create basic authentication if needed
from flask_cors import CORS # import flask library Cross-Origin Resource Sharing that is a mechanism that uses additional HTTP headers to tell a browser to let a web application running at one origin (domain) have permission to access selected resources from a server at a different origin
app = Flask(__name__)
CORS(app) #set-up cors for my app
#if you want use basic authentication you need set-up username and password
app.config['BASIC_AUTH_USERNAME'] = 'admin'
app.config['BASIC_AUTH_PASSWORD'] = 'password'
basic_auth = BasicAuth(app)#set-up username and password for my app but in this case I'm not specifying yet in which API use them
#app.route('/api/v1/resources/add_messages', methods=['POST'])#create my POST api
#basic_auth.required# set-up basic authentication for this API, comment out if not needed
def update_credential ():
json_credential=request.get_json()#get the JSON sent via API
print (json_credential["message"])#get the node "message" of my JSON
###########
#code to write in your file, you need write the json_credential["message"]
###########
return ("ok")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1024, threaded=True)#start my flask app with local_host IP and specific port, if you don't specify the port it will run in the default port
In this case the JSON Input should be:
{"message":"your text"}
Please let me know if something is not clear, I even try this code on my local and the JSON is passed without problems.....
So you need run your python script and see that the API is running, if you had no JSON to send and was just a simple API that give back information you should have used even Chrome but in this case that you need send some JSON data I would advice you to use Postman.
See screenshot example:

how to save output from the shell by using python flask [duplicate]

This question already has an answer here:
Testing code that requires a Flask app or request context
(1 answer)
Closed 5 years ago.
First of all, I am very new at programming.
I am trying to save a variable from bash shell
>curl http://169.254.169.254/latest/meta-data/
this line would return data such as local-ipv4. And I am trying to use phython and flask to save those variables. I wrote
from flask import Flask, request
app = Flask(__name__)
#app.route('/')
def testRequest():
url1 = "http://169.254.169.254/latest/meta-data/"
name1 = request.get(url1)
nameText = name1.text
return nameText
testOutput = testRequest()
print testOutput
But this gives me runtime error : working outside of the request context.
can someone guide me to where to look for possible solution?
To things to comment here:
First, it seems that you'll be just fine by using requests, a highly recommended library for HTTP requests. With it you could do:
import requests
url = "http://169.254.169.254/latest/meta-data/"
resp = requests.get(url)
print resp.text
Regards to the error you're getting runtime error : working outside of the request context, is because by testOutput = testRequest() you're calling a method that's part of the Flask app app. Another thing related to the error is that you never ran the Flask app. To do this, include this at the end of your code.
if __name__ == '__main__':
app.run()
But again, Flask is rather a web framework that it's useful to create web sites, APIs, web apps, etc. It's very useful, but I don't think you may need it for what you're trying to achieve.
Further info about requests and Flask:
http://docs.python-requests.org/
http://flask.pocoo.org/
Since you only need to make an HTTP GET request and print the response, you don't need Flask. You can use the urllib standard library to send the GET request (https://docs.python.org/3/library/urllib.request.html):
import urllib.request
def testRequest():
url1 = "http://169.254.169.254/latest/meta-data/"
response = urllib.request.urlopen(url1)
nameText = response.read().decode('utf-8')
return nameText
testOutput = testRequest()
print testOutput

Why does this default route redirect?

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.

Categories

Resources