Client code:
import requests
import json
url = 'http://127.0.0.1:5050/login'
user = "newUser"
password = "password"
headers = {'content-type': 'application/json'}
response = requests.post(url, data={"user": user,"pass": password}, headers = headers)
Server code:
from flask import Flask, request, make_response
app = Flask(__name__)
@app.route('/login', methods=['GET','POST'])
def login():
if request.method == 'POST':
username = request.form.get("user")
password = request.form.get("pass")
//more code
return make_response("",200)
if __name__ == "__main__":
app.run(host = "127.0.0.1", port = 5050)
The problem is that my username and password are always None.
I also tried using:
content = request.get_json(force = True)
password = content['pass']
and
request.form['user']
When I print the content I have: < Request 'http://127.0.0.1:5050/login' [POST]> .So I cannot find the json send from the client.
EDIT:
I did add json.dumps and used request.get_json() and it worked
You need to set the request content type to application/json for the . json property and . get_json() method (with no arguments) to work as either will produce None otherwise.
Parses the incoming JSON request data and returns it. By default this function will return None if the mimetype is not application/json but this can be overridden by the force parameter.
You are sending form encoded data, not JSON. Just setting the content-type doesn't turn your request into JSON. Use json=
to send JSON data.
response = requests.post(url, json={"user": user,"pass": password})
Retrieve the data in Flask with:
data = request.get_json()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With