Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda