My flask app is getting a bad request error

Viewed 24

My flask app is getting a strange bad request error.

127.0.0.1 - - [21/Sep/2022 10:37:05] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Sep/2022 10:37:33] "GET /test?%22[{\%22name\%22:\%22Tanner\%22,\%22title\%22:\%22Analyst\%22,\%22role\%22:\%22Head\%22,\%22reports\%22:\%22Jesus\%22,\%22fact\%22:\%22I%20like%20to%20code\%22}]%22 HTTP/1.1" 400 -

It seems to be converting my inputs array into a url.

The purpose is to

  1. take the user inputs
  2. form them into an object
  3. append that object to an array
  4. turn the array into a json string that is then converted into a python dictionary.

This is the relevant code from my app.py

@app.route('/')
def index():
    return render_template('index.html')

    @app.route('/test', methods=['POST'])
    def test():
        output = request.get_json()
        print(f'output: {output}, output type: {type(output)}') # This is the output that was stored in the JSON within the browser
        response = app.response_class(
            response=json.loads(output),
            status=200,
            mimetype='application/json'
        )
        return response 

This is from app.js

//for display purposes only 
    console.warn('added' , {persons} );
    let pre = document.querySelector('#msg pre');
    pre.textContent = '\n' + JSON.stringify(persons, '\t', 2);
    const people = JSON.stringify(persons);
    $.ajax({
        url: '/test',
        type: "POST",
        type: "GET",
        contentType: "application/json",
        data: JSON.stringify(people)
    });

    //saving to localStorage
    //localStorage.setItem('SurveyResults', JSON.stringify(names) );
}
document.addEventListener('DOMContentLoaded', ()=>{
    document.getElementById("btn").addEventListener('click', addPerson);
});

"persons" is the array of objects, the inputs are name, title, role, reports to, and fun fact

0 Answers
Related