I am trying to make a javascript request to a python backend rest, and getting errors I cannot resolve. I am trying to test if a listname is in a request.json object that I am passing through the axios data, but python either can't see it or throws an error.
Here is my python code:
@app.route('/', methods=['GET', 'DELETE', 'POST'])
def profits():
if request.method=='GET':
if 'listname' in request.json():
print 'inside listname == True!!!'
conn = psycopg2.connect(database = "profitnloss3", user = "patientplatypus", password = "*************")
cur = conn.cursor()
sql = 'SELECT * FROM ledger WHERE listname = %s'
params = (request.json['listname'])
cur.execute(sql, params)
conn.commit()
data = cur.fetchall()
conn.close()
return jsonify(data)
else:
print 'inside listname == False!!!'
conn = psycopg2.connect(database = "profitnloss3", user = "patientplatypus", password = "*******************")
cur = conn.cursor()
sql = 'SELECT * FROM ledger'
cur.execute(sql)
conn.commit()
data = cur.fetchall()
conn.close()
return jsonify(data)
The relevant line that is giving me trouble is if 'listname' in request.json():
Here is the axios call that I am making in the front end:
axios({
method: 'get',
url: 'http://localhost:5000/',
headers: {
'Content-type': 'application/json'
},
data:{
'listname': this.ledgername
}
})
.then((response)=>{
console.log('this is the response from the python server on a get request of listname ', response);
})
.catch(error=>{
console.log('here is the error on a get request from the python server of listname ', error);
})
The error that I am getting is:
TypeError: 'NoneType' object is not callable
If I instead use if 'listname' in request.json:
TypeError: argument of type 'NoneType' is not iterable
I have also tried using the form variable (ie request.form) but the values are then completely ignored from the axios post even though an error is not thrown (I guess it doesn't see it as form data).
I do not know where to go from here, any help would be appreciated.
EDIT:
My import header at the top of my python file is
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS, cross_origin
import sqlite3
import psycopg2
import os
from os.path import join, dirname
from dotenv import load_dotenv
from models.shared import db
from models.profits import Profit