If I send run this program to do a HTTP Post to my Flask server, which I know returns a 200 response:
import requests
import json
dump= '{"on": false}'
r = requests.post('http://127.0.0.1:5000', data=dump,
headers={'Content-Type': 'application/json'})
And my Flask server's code:
from flask import Flask
from flask import request, jsonify
import requests
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def signal():
if request.method == 'POST':
content = request.get_json()
return jsonify(content)
print(jsonify(content))
r = requests.put("http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state/", jsonify(content))
else:
return 'Hello, world!'
if __name__ == '__main__':
app.run(debug=True)
I want to print the data to the console, then send it over to a bridge on the network using a HTTP PUT. Neither of these are working, and I'm not sure why.
You need to return at the very end of the function
#app.route('/', methods=['GET', 'POST'])
def signal():
if request.method == 'POST':
content = request.get_json()
print(content)
r = requests.put("http://192.168.1.102/api/F5La7UpN6XueJZUts1QdyBBbIU8dEvaT1EZs1Ut0/lights/5/state/", content)
return jsonify(content)
else:
return 'Hello, world!'
Note: You probably are over-using the jsonify function because the jsonify() function in flask returns flask.Response() object, and not a JSON string that you would POST or PUT to another service.
Related
Here is the code
import os
import redis
import flask
import json
import urllib.parse
from flask import Flask, Response, request, render_template, abort
from flask_cors import CORS, cross_origin
#from flask.ext.cors import CORS, cross_origin
app = Flask(__name__)
app.config['CORS_HEADERS'] = 'Content-Type'
redis_handle = redis.Redis('localhost')
requiredFields = ("id", "title", "name") # fields required for user object
#app.route('/')
#cross_origin()
def hello():
return 'Hello World!'
#app.route('/users/<user_id>', methods=['GET'])
#cross_origin()
def get_user(user_id):
response = {}
# user_id = request.args.get("id")
user = redis_handle.get(user_id)
if not user:
response["msg"] = "no user found"
return Response(json.dumps(response), status=404, mimetype="application/json")
return user
#app.route('/users', methods=['POST'])
#cross_origin()
def save_user():
data = request.get_json(force=True)
response = {}
if all(field in data for field in requiredFields):
redis_handle.set(data["id"], json.dumps(data))
return Response(status=201)
else:
missing_key = str([val for val in requiredFields if val not in dict(data).keys()])
response["msg"] = "required key " + missing_key + " not found"
return Response(json.dumps(response), status=400)
#app.route('/users/<user_id>', methods=['DELETE'])
#cross_origin()
def delete_user(user_id):
response = {}
resp = redis_handle.delete(user_id)
if resp == 0:
response["msg"] = "no such entity found"
status = 404
else:
response["msg"] = "Delete op is successful"
status = 200
return Response(json.dumps(response), status=status)
#app.route('/clear', methods=['GET'])
#cross_origin()
def clear_data():
redis_handle.flushall()
return "ok!"
if __name__ == "__main__":
app.run(debug=True)
As of my knowledge, I have even included the method = "POST" as well but still don't know what is going wrong.
I tried to create a small crud application using redis, python, flask but couldn't encountering this issue. Can someone tell me where and what am I doing wrong?
Browsers don't run POST methods outside of a <form> entry or AJAX function. Therefore, you're running a GET, which "isn't allowed".
Unclear what you expected, but to see all users, you'll need to edit your route to first add the GET method, then if so, return a response that returns/renders all users rather than checking the request json body, which won't exist for GET requests
If you only wanted to get one user, edit the url to include the user ID
The browser will use the GET method for URLs that you input in URL/search bar but you don't have any function decorated with #app.route('/users', methods=['GET']).
If you want to create a user with POST /users then it would be easier to use some HTTP client like https://www.postman.com, https://insomnia.rest, etc. or even fetch in the browser's console.
I want to access headers for a certain API calls outside of its api route. I tried using the app_context and test_request_context but it doesn't seem to work.
from flask import Flask, request
app = Flask("app")
def access_header_for_something():
with app.test_request_context():
with app.app_context():
print(request.headers.get("Authorization"), request.host, request.path)
#app.route('/')
def index():
access_header_for_something()
return 'hello'
if __name__=="__main__":
app.run(debug=True)
Any suggestions would be really helpful
The above code snippet work with slight tweak:
from flask import Flask, request
app = Flask("app")
def access_header_for_something():
with app.app_context():
print(request.headers.get("Authorization"), request.host, request.path)
#app.route('/')
def index():
access_header_for_something()
return 'hello'
if __name__=="__main__":
app.run(debug=True)
So I am trying to send certain values to my Flask API streamlit application, but it appears that it is not executing a post request. Right now, the post request code I have for my main.py does not work because I am getting a TypeError: 'NoneType'.
app.py :
import requests
import json
import streamlit as st
...
api_url = "http://127.0.0.1:5000/" # Flask url
create_row_data = {'name': name, 'type': get_type(name), 'token_value': token_value,
'external': external, 'start': start, 'end': end, 'step': step}
print(create_row_data)
# The terminal cannot print out the value of r and it appears that it cannot send the post request as well
r = requests.post(url=api_url, json = create_row_data)
print(r)
Output of print(create_row_data) in app.py:
{'name': 'session', 'type': 'area_chart', 'token_value': 'G0as7vjk1ksuxn94',
'external': False, 'start': datetime.datetime(2021,7,1,14,9,7,322438), 'end': datetime.datetime(2021,7,8,14,9,7,322441), 'step': '1d'}
main.py:
from flask import Flask
from flask import jsonify
from flask import request
...
import requests, json
#app.route('/', methods=['GET', 'POST'])
def get_data():
if request.method == 'GET':
return "ok", 200
if request.method =='POST':
p_name = request.json['name']
p_type = request.json['type']
...
p_end = request.json['end']
p_step = request.json['step']
create_row_data = {'p_name': str(p_name), 'p_type': str(p_type), ... , 'p_end': str(p_end), 'p_step': str(p_step)}
print(create_row_data)
response = requests.post(url, data=json.dumps(create_row_data), headers= {'Content-type': 'application/json'}
return response.content
From my understanding, you need to post data to 127.0.0.1 and process it to create a table from app.py using flask_restful to create an API endpoint so that you can post data. The code for main.py would be:
from flask import Flask
from flask import Flask, request
from flask_restful import Resource, Api
from flask import jsonify
from flask import request
import requests, json
app = Flask(__name__)
api = Api(app)
#app.route('/', methods=['GET', 'POST'])
def get_data():
if request.method == 'GET':
return "ok", 200
class create_row_data(Resource):
def post(self):
response = request.get_json()
p_name = response['name']
p_type = response['type']
...
# code to process your data and to create a table
# return something for your client application
return True
api.add_resource(create_row_data, '/create_row_data/')
No changes are needed for your client app.py except the url which now changes to http://127.0.0.1:5000/create_row_data/
i am getting error 404 while running this
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. how do i need to run the server ? please help
from flask import Flask
from flask import render_template
from flask import request
app = Flask(__name__, template_folder="templates/")
app.route("/login", method=['POST', 'GET'])
def index():
greeting = "Hello World"
if request.method == "POST":
name = request.form['name']
greet = request.form['greet']
greeting = f"{greet}, {name}"
return render_template("index.html", greeting=greeting)
else:`enter code here`
return render_template("hello_form.html")
if __name__ == "__main__":
app.run(debug=True)
You need to replace
app.route("/login", method=['POST', 'GET'])
By
#app.route("/login", methods=['POST', 'GET'])
Edit: also, methods
It's the most basic thing to ask but #app.route('/') is not working on my Linux server.
Below is the code :
from flask import Flask, jsonify, request
from app import models
import json
import time
app = Flask(__name__)
app.url_map.strict_slashes = True
#app.route('/')
def blank():
return 'Hello ABC!'
#app.route('/driftking')
def blank2():
return 'Hello driftking!'
# dynamic route
#app.route("/test/<search_query>")
def search(search_query):
return search_query
#app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return "POST METHOD"
elif request.method == 'GET':
return "GET REQUEST ARE NOT ALLOWED"
if __name__ == '__main__':
app.run(debug=True)
app.run()
Very basic app, all's working fine on local machine but not on my linux server.
E.g. if I load http://xxx.xxx.xxx.xxx/projectname ---- it shows Hello ABC!
If I load http://xxx.xxx.xxx.xxx/projectname/driftking -- it redirects me to http://xxx.xxx.xxx.xxx ( i.e my server's homepage)
If I load http://xxx.xxx.xxx.xxx/projectname/test/search -- 404 error not found
If I load http://xxx.xxx.xxx.xxx/projectname/login -- it redirects me to http://xxx.xxx.xxx.xxx ( i.e my server's homepage)
127.0.0.1 - - [24/Nov/2017 19:37:01] "POST //login HTTP/1.1" 405 -
^^This is what I get on terminal. I don't understand why I get two leading slashes everytime.
But If do http://xxx.xxx.xxx.xxx/projectname/insert-any-word/login , my post req get's executed. At the same time on local machine i dont get two leading slashes // to the path and thus the request get's processed.
There are a couple of things wrong but try this, you'll need a proxy to help with the rerouting.
from flask import Flask, jsonify, request
from app import models
import json
import time
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.url_map.strict_slashes = False
#app.route('/')
def blank():
return 'Hello ABC!'
#app.route('/driftking')
def blank2():
return 'Hello driftking!'
# dynamic route
#app.route("/test/<search_query>")
def search(search_query):
return search_query
#app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return "POST METHOD"
elif request.method == 'GET':
return "GET REQUEST ARE NOT ALLOWED"
if __name__ == '__main__':
app.run(debug=True)