0

Hi Can someone help me please, I'm probably doing something stupid here (blame having a cold) but I thought I'd ask on here while I "walk away" for a bit..

I'm messing around with Flask in Python and have a request coming from a React App (I'm also messing with)

Here is my example API call...

@app.route('/addnewclient', methods=['GET', 'POST'])
def addNewClient(dbName = 'customers'):
    account_dict = request.form.to_dict()
    print(account_dict)

The data I am sending from the app for example:

{name: 'Joe Bloggs'}

The above request.form.to_dict() prints it as :

{'{name: 'Joe Bloggs'}': ''}

Treating the entire data as the Key I guess... Its POST request and I've tried various other things like args or not using to_dict etc (ImmutableMultiDict).

What am I doing wrong here?

FYI, my javascript on the other side is:

const handleClick = async() => {
    const API_URL = 'http://localhost:1000';
    let path = '/addnewclient'
      try {
        const response = await axios.post(
            API_URL + path,
            JSON.stringify(form)
            ); 
        console.log(response.data)
      } catch (error) {
        console.error(error);
      }
}
Lawrence Ferguson
  • 179
  • 2
  • 4
  • 11

1 Answers1

0

In order to get JSON data as input in flask route, you can use request's get_json() method.

Example:

@app.route('/addnewclient', methods=['GET', 'POST'])\
def addNewClient(dbName = 'customers'):\
        account_dict = request.get_json()\
    print(account_dict)

Helpful links: https://www.programcreek.com/python/example/64945/flask.request.get_json Get json from request flask