Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

93
Vistas
Body of JS Request not Appearing in Flask

I'm attempting to make a POST request from a JS script to a Flask server on pythonanywhere like so:

const response = await fetch('http://myusername.pythonanywhere.com/', {
        method: 'POST',
        mode: 'no-cors',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            track: track
        })
    });

Where track is an nx10 array of numbers. I have successfully made requests to the server via Insomnia with a body like so:

{
    "track": [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10.234], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]
}

and printed the request's body contents to the logs where it looks like this:

2021-11-16 15:57:14 Test message!
2021-11-16 15:57:14 <Request 'http://myusername.pythonanywhere.com/' [POST]>
2021-11-16 15:57:14 {'track': [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10.234], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]}

However, when I try to make a request from my web application with the first piece of code, I get this in the logs:

2021-11-16 15:54:21 Test message!
2021-11-16 15:54:21 <Request 'http://myusername.pythonanywhere.com/' [POST]>
2021-11-16 15:54:21 None

Server Code:

@app.route('/', methods=['POST'])
def handle_post():
    req_data = request.get_json()
    print("Test message!")
    print(request)
    print(req_data)

    data = req_data['track']
    trackData = np.array(data)
    grade = analysisV2(trackData)
    return jsonify({"grade": grade})

I have no idea why one request works but the other doesn't. I have also tried hard coding the same data I am using in Insomnia into the JS request body with no difference. Does anyone see something I might be missing?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I managed to figure out what was going on in case anyone is wondering and happens to be doing something similar.

There were a couple issues here:

  1. Adding mode: 'no-cors' to the request prevents the server from responding with any actual data, so this must be removed.
  2. The get_json() method from Flask wasn't working for whatever reason in this case, so I just used the json.loads(request.get_data()) method instead. In the response, the json.dumps() method was also used instead of .jsonify().
  3. The client must send a request with the 'Access-Control-Allow-Origin': '*' header.
  4. The server also needs to have CORS enabled with from flask_cors import CORS and CORS(app)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda