1

In Express.js I can configure the whole app to support mimetype json or form with this:

app.use(bodyParser.urlencoded({
    extended: true
}));

app.use(bodyParser.json());

Then in the routers, I just get the values with:

function (req, res) {
      fields  =req.body
}

In Flask, I must to use something like this for every route:

if request.mimetype == 'application/json':
   res = request.get_json()
else:
   res = request.form

firstname = res['firstname']
lastname = res['lastname']

but I don't like using if and else this way. Is there another automatic or cleaner way?

davidism
  • 121,510
  • 29
  • 395
  • 339
stackdave
  • 6,655
  • 9
  • 37
  • 56

1 Answers1

1

Flask got hooks before_request and after_request. You can do your transformation in before_request method.

@app.before_request
def before_request():
    # do your work here
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76