0

This is what i get

b'{"data": "https://files.slack.com/files-pri/T03HPFD2P-F2RU2S4R0/scher7-om-14.52.47.png"}'

when i print my request data from a flask POST web service.

print(request.data)

But when i do url = request.form.get('data', None) I get value of url variable None Why?

prashantitis
  • 1,797
  • 3
  • 23
  • 52
  • what is `b` in this json data – prashantitis Oct 20 '16 at 13:23
  • How are you posting the data? Please include any relevant code, like html forms, curl/requests syntax, etc. – sytech Oct 20 '16 at 13:41
  • I am posting data using `req = requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})` – prashantitis Oct 20 '16 at 13:43
  • You should paramaterize your request like this: `req = requests.post(url, json=payload, headers={'Content-Type': 'application/json'})` Then access it in your flask application as `request.json` -- The problem is your not submitting FORM data, therefore `request.form` will not have anything! – sytech Oct 20 '16 at 13:47
  • and how can i submit it? – prashantitis Oct 20 '16 at 13:54
  • What do you mean by "submit it" ? – sytech Oct 20 '16 at 13:55
  • you just said, that I am not submitting form data, but if that is the situation, then in my flask service,,,after receiving request, doing this `print(request.data)` should not print data – prashantitis Oct 20 '16 at 13:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126237/discussion-between-sytech-and-guru). – sytech Oct 20 '16 at 13:57

2 Answers2

2

Your problem is that you sent the request as follows:

req = requests.post(url, data=json.dumps(payload), headers={'Content-Type': 'application/json'})

When sending json data, you should format the request in requests with the json parameter as follows:

req = requests.post(url, json=payload)

Further, in your flask app, you can access submitted json data via request.json

Because the content type is application/json -- it will not appear in request.form -- Data will appear in request.form when a request is received with an appropriate content type such as multipart/form-data

davidism
  • 121,510
  • 29
  • 395
  • 339
sytech
  • 29,298
  • 3
  • 45
  • 86
0

What's the Content-Type header in your POST request?

As described in its docs: request.form is a dict contains data parsed from POST or PUT form, while request.data contains the incoming request data as string in case it came with a mimetype Flask does not handle.

For the following code:

# -*- coding: utf-8 -*-

from flask import Flask, request

app = Flask("Test")

@app.route("/ping", methods=['POST'])
def ping():
    print "Data: ", request.data
    print "Form: ", request.form
    return "pong"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7070, debug=False)
  • Curl with default Content-Type, there's no data in request.data:

    curl -X POST -H "Content-Type: a/b" --data 'data=https://files.slack.com/files-pri/T03HPFD2P-F2RU2S4R0/schermafbeelding-2016-01-27-om-14.52.47.png' http://localhost:7070/ping
    

    Output:

     * Running on http://0.0.0.0:7070/ (Press CTRL+C to quit)
     Data:
     Form:  ImmutableMultiDict([('data', u'https://files.slack.com/files-pri/T03HPFD2P-F2RU2S4R0/schermafbeelding-2016-01-27-om-14.52.47.png')])
     127.0.0.1 - - [20/Oct/2016 21:40:41] "POST /ping HTTP/1.1" 200 -
    
  • But curl with a unknown Content-Type header, there's no data in request.form:

    curl -X POST -H "Content-Type: a/b" --data 'data=https://files.slack.com/files-pri/T03HPFD2P-F2RU2S4R0/schermafbeelding-2016-01-27-om-14.52.47.png' http://localhost:7070/ping
    

    Output:

    Data:  data=https://files.slack.com/files-pri/T03HPFD2P-F2RU2S4R0/schermafbeelding-2016-01-27-om-14.52.47.png
    Form:  ImmutableMultiDict([])
    127.0.0.1 - - [20/Oct/2016 21:43:52] "POST /ping HTTP/1.1" 200 -
    

So if you want your form data in request.form, make sure the Content-Type is the one of those Flask can handle.

shizhz
  • 11,715
  • 3
  • 39
  • 49