Robin saint georges
Robin saint georges

Reputation: 45

Why my request works with postman but not with vue.js axios?

I've made a back using Flask, and a front using vue.js,

Why I make the request with postman it returns what I want but not with axios ...

for exemple :

    this.$axios
      .post('http://127.0.0.1:5000/getUserDataByMail', { mail: '[email protected]' })
      .then(response => {
        console.log('this.userData')
        console.log(response.data)
        this.userData = response
        }
      )

Is treated by :

@app.route('/getUserDataByMail', methods = ['GET', 'POST'])
def getUserDataByMail():
    args = request.args
    mail = args['mail']
    return jsonify(mail)
    cur = mysql.connection.cursor()
    dataCur = cur.execute('select * from userdata where email like "' + mail + '"')
    if dataCur > 0:
        data = cur.fetchall()
        cur.close()
        return jsonify(data)
    cur.close()

But this result in an error 400 ...

POSThttp://127.0.0.1:5000/getUserDataByMail [HTTP/1.0 400 BAD REQUEST 4ms] Uncaught (in promise) Error: Request failed with status code 400

Help me I'm losing my mind ! (:

Upvotes: 0

Views: 1540

Answers (1)

Phil
Phil

Reputation: 164795

Axios by default posts an application/json request body.

To read JSON payloads in Flask, you use request.json

content = request.json
mail = content["mail"]

I can only assume Postman works because you were posting an application/x-www-form-urlencoded request body or using URL query parameters.

To match what you're doing in Axios, make sure you post JSON

postman

Upvotes: 1

Related Questions