{"id":1566,"date":"2021-11-12T14:36:19","date_gmt":"2021-11-12T14:36:19","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1566"},"modified":"2022-02-22T15:31:47","modified_gmt":"2022-02-22T15:31:47","slug":"python-flask-post-content","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2021\/11\/python-flask-post-content\/","title":{"rendered":"Python Flask\u83b7\u53d6POST\u8bf7\u6c42\u6570\u636e\u5185\u5bb9"},"content":{"rendered":"

Python Flask\u83b7\u53d6POST\u8bf7\u6c42\u6570\u636e\u5185\u5bb9.md
\n<\/p>\n

\u6709\u65f6\u5019\uff0c\u6211\u4eec\u8c03\u8bd5\u4e00\u4e2a\u8fdc\u7a0b\u63a5\u53e3\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u6587\u6863\uff0c\u53ea\u80fd\u901a\u8fc7\u6293\u53d6POST\u8bf7\u6c42\u7684\u6570\u636e\u5185\u5bb9\u6765\u786e\u5b9a\u5176\u683c\u5f0f\uff0c\u7136\u540e\u624d\u80fd\u505a\u51fa\u5bf9\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002
\nFlask \u5904\u7406POST\u8bf7\u6c42\u67093\u79cd\u65b9\u6cd5\uff1a<\/p>\n

\n
request.json or request.get_json()  # \u5904\u7406 application\/json   \nrequest.form #\u5904\u7406 multipart\/form-data\nrequest.data #\u5904\u7406\u88f8\u6570\u636e<\/code><\/pre>\n<\/div>\n

\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\n
from flask import Flask, request, json\n\n\napp = Flask(__name__)\n\n\n@app.route('\/')\ndef hello_world():\n    return 'hello world'\n\n\n@app.route('\/register', methods=['POST'])\ndef register():\n    print('===request header: ')\n    print(request.headers)\n    if  'application\/json' in request.headers['Content-Type']:\n        print('===recevied Content-Type: application\/json')\n        print(request.json)\n    elif 'form' in  request.headers['Content-Type']:\n        print('===recevied Content-Type', request.headers['Content-Type'])\n        print(request.form)\n    else:\n        print('===stream read')\n        #print(request.stream.read())\n        print(request.data)\n    return 'OK'\n\n\nif __name__ == '__main__':\n    app.run(port=5000, debug=True)<\/code><\/pre>\n<\/div>\n
\u6765\u81ea\u4e3a\u77e5\u7b14\u8bb0(Wiz)<\/a><\/div>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Python Flask\u83b7\u53d6POST\u8bf7\u6c42\u6570\u636e\u5185\u5bb9<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Python Flask\u83b7\u53d6POST\u8bf7\u6c42\u6570\u636e\u5185\u5bb9.md \u6709\u65f6\u5019\uff0c\u6211\u4eec\u8c03\u8bd5\u4e00\u4e2a\u8fdc\u7a0b\u63a5\u53e3\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u6587\u6863\uff0c\u53ea\u80fd\u901a\u8fc7\u6293\u53d6POST\u8bf7\u6c42\u7684\u6570\u636e\u5185\u5bb9\u6765\u786e\u5b9a\u5176\u683c\u5f0f\uff0c\u7136\u540e\u624d\u80fd\u505a\u51fa\u5bf9\u5e94\u7684\u5904\u7406\u903b\u8f91\u3002 Flask \u5904\u7406POST\u8bf7\u6c42\u67093\u79cd\u65b9\u6cd5\uff1a request.json or request.get_json() # \u5904\u7406 application\/json request.form #\u5904\u7406 multipart\/form-data request.data #\u5904\u7406\u88f8\u6570\u636e \u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a from flask import Flask, request, json app = Flask(__name__) @app.route(‘\/’) def hello_world(): return ‘hello world’ @app.route(‘\/register’, methods=[‘POST’]) def register(): print(‘===request header: ‘) print(request.headers) if ‘application\/json’ in request.headers[‘Content-Type’]: print(‘===recevied Content-Type: application\/json’) print(request.json) elif ‘form’ in request.headers[‘Content-Type’]: print(‘===recevied Content-Type’, request.headers[‘Content-Type’]) […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1566"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1566"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1566\/revisions"}],"predecessor-version":[{"id":1567,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1566\/revisions\/1567"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}