sshevlyagin
sshevlyagin

Reputation: 1387

flask returns 400 on post request from postman

I'm doing a flask course on lynda and had trouble with an example. https://www.lynda.com/Flask-tutorials/Web-API-Development-Flask/521200-2.html

I'm doing a simple post and it works when from a basic webform but fails from postman. I think it's because postman encodes the request different, but can't figure out why it does that or why flask can't read it.

Postman: enter image description here

Request intercepted in Charles:

POST /api/candidate HTTP/1.1
Host: localhost:5000
Content-Length: 248
Postman-Token: 285ed4d8-2af6-0eb6-8de7-e8ecb41fb86e
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

------WebKitFormBoundary3Q45bnchdpXxtZbw
Content-Disposition: form-data; name="first_name"

Susan
------WebKitFormBoundary3Q45bnchdpXxtZbw
Content-Disposition: form-data; name="last_name"

Kruger
------WebKitFormBoundary3Q45bnchdpXxtZbw--

Return:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>

Request from a simple POST web form:

POST /api/candidate HTTP/1.1
Host: localhost:5000
Content-Length: 33
Cache-Control: max-age=0
Origin: null
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

first_name=Mickey&last_name=Mouse

Return (correct):

{
    "id": "a7c2f6a3-e8d2-4c53-be97-ccf72a04295d",
    "url": "/api/candidate/a7c2f6a3-e8d2-4c53-be97-ccf72a04295d"
}

Relevant python code: @app.route("/api/candidate", methods=["POST"])

def add_candidate():
    print dict(request.form)
    first_name = request.form["first_name"]
    last_name = request.form["last_name"]
    new_candidate_id = DATA_PROVIDER.add_candidate(first_name, last_name)
    return jsonify({
        "id": new_candidate_id,
        "url": url_for("candidate_by_id", id=new_candidate_id)
    })

Output of print when it fails:

{'------WebKitFormBoundary3Q45bnchdpXxtZbw\r\nContent-Disposition: form-data; name': [u'"first_name"\r\n\r\nSusan\r\n------WebKitFormBoundary3Q45bnchdpXxtZbw\r\nContent-Disposition: form-data; name="last_name"\r\n\r\nKruger\r\n------WebKitFormBoundary3Q45bnchdpXxtZbw--\r\n']}

Output of print when it works:

{'first_name': [u'Mickey'], 'last_name': [u'Mouse']}

Upvotes: 3

Views: 8501

Answers (1)

salezica
salezica

Reputation: 76889

The working request from your web form has a Content-Type header with value application/x-www-form-urlencoded.

Your screenshot shows that Postman is using form-data:

Postman

Try selecting application/x-www-form-urlencoded.

Upvotes: 8

Related Questions