0

This might be a silly question but has been frustratign me for hours.

I am having requests act as my front end and beene trying to execute posts that talk to a Flask backend. The requests code is given below:

import requests
import json
data = {
    "username": "user",
    "password": "pass"
}
data = json.dumps(data)
b = requests.post(url='http://127.0.0.1:5000/login/', data=data)
print b.text

My flask backend code is given below:

from flask import Flask
from flask import request, url_for, render_template
app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('index.html')

@app.route("/login/", methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        abc = request.get_json()
        print abc['username']
        print abc["password"]
        return abc["password"]
    else:
        return render_template('index.html')

if __name__ == "__main__":
    app.run()

When I run Flask as my backend server and run the requests code, I get the following error:

print abc["password"]
TypeError: 'NoneType' object has no attribute '__getitem__'

I do not understand why abc is of type None. Simulating the same scenario using Postman and Flask works.

lordlabakdas
  • 1,163
  • 5
  • 18
  • 33

0 Answers0