Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
Flask-restful api post gives back empty body via javascript

Having a local database running via python, I'm trying to do some api requests to it via a website. First tried both GET's and POST's as python unittest, which worked fine. Then using javascript; GET function working perfect but my POST function, whatever I do, sends over an empty body to the python function (variable data in python code) or in other words a dict with nothing in it, while I'm passing data through it.

relevant python snippet:

conn = sq3.connect("temp.db", check_same_thread=False)
class Login(Resource):
def post(self):
    data = flask.request.form.to_dict()
    lst = conn.execute(f"""SELECT AccountID, Role FROM Account 
    WHERE Email = \"{data['email']}\"
    AND PassW = \"{data['passw_hashed']}\"
    """).fetchall()
    return {"LoginSucces": [{"AccountId": e[0], "Role": e[1]} for e in lst]}

app = flask.Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": '*'}})
api = Api(app)
api.add_resource(Login, "/login")
app.run(port=8080)

Javascript:

function req_login(){
    let email_p = document.getElementById("login_email").value
    let passw = document.getElementById("login_passw").value

    let data = JSON.stringify({email: email_p,passw_hashed: passw.hashCode()});

    const request = new XMLHttpRequest();
    request.open("POST", IP+"/login");
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader('Content-Type', 'application/json');
    request.send(data);
    request.onload = (e) => {
        let jsret = JSON.parse(request.response);
        let topkey = Object.keys(jsret);
        let data_arr = jsret[topkey];
        alert(data_arr['AccountId']);
    }
}

Tried with manual data in javascript as well to see if reading out the data was the problem, without succes with the following bit of code:

const data = `{email: "tst@gmail.com", passw: "testtest123"}`;

Where does it go wrong, what can I try/change. If you need any more info, send in comment

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

After quite a bit of debugging, I found the solution myself. Hope it helps someone:

replace data = flask.request.get_json()

with data = flask.request.json

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!