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

97
Views
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 answers
Answer question

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 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!