본문으로 바로가기

파이썬 Flask HTTP Request 파라미터

category Application/Flask 2020. 8. 20. 22:06
728x90

예제1

 

GET, POST 파라미터에 접근하는 방법에 대한 예제입니다.

 

app.py

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    return 'hello'

@app.route('/getReq', methods=['GET'])
def r():
    return request.args.get('page')

@app.route('/postReq', methods=['POST'])
def pr():
    if request.form.get('secret') == '0820':
        return 'today'
    else:
        return request.form.get('secret')

if __name__ == '__main__':
    app.run(debug=True)

 

test.py

import requests

url = 'http://localhost:5000/getReq'
url2 = 'http://localhost:5000/postReq'

res = requests.get(url, params={'page': '999'})
res2 = requests.post(url2, data={'secret': '0820'})

print(f'get request response: {res.text}')
print(f'post request response: {res2.text}')
get request response: 999
post request response: today

 

 

GET 파라미터는 request.args 으로 접근합니다.

request.args.get('page')

 

POST 파라미터는 request.form 으로 접근합니다.

request.form.get('secret')

 

 

예제2

 

request.values 를 사용하면 GET, POST 모두 접근할 수 있습니다.

또한 requests.args.values, request.args.getlist, request.form.getlist 를 이용해서 Key, Value 를 확인할 수 있습니다.

 

app.py

from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def index():
    return 'hello'

@app.route('/getReq', methods=['GET'])
def r():
    return request.args.get('page')

@app.route('/postReq', methods=['POST'])
def pr():
    if request.form.get('secret') == '0820':
        return 'today'
    else:
        return request.form.get('secret')

@app.route('/anyReq', methods=['GET', 'POST'])
def anyreq():
    print(request.values)
    if request.method == 'POST':
        print(request.args.getlist)
        return 'post'
    else:
        print(request.form.getlist)
        return 'get'


if __name__ == '__main__':
    app.run(debug=True)
CombinedMultiDict([ImmutableMultiDict([('getData', 'get')]), ImmutableMultiDict([])])
<bound method MultiDict.getlist of ImmutableMultiDict([('getData', 'get')])>
<bound method MultiDict.getlist of ImmutableMultiDict([])>
127.0.0.1 - - [20/Aug/2020 21:52:52] "?[37mGET /anyReq?getData=get HTTP/1.1?[0m" 200 -
CombinedMultiDict([ImmutableMultiDict([]), ImmutableMultiDict([('postData', 'post')])])
<bound method MultiDict.getlist of ImmutableMultiDict([])>
127.0.0.1 - - [20/Aug/2020 21:52:53] "?[37mPOST /anyReq HTTP/1.1?[0m" 200 -

 

test.py

import requests

url = 'http://localhost:5000/getReq'
url2 = 'http://localhost:5000/postReq'
url3 = 'http://localhost:5000/anyReq'

res = requests.get(url, params={'page': '999'})
res2 = requests.post(url2, data={'secret': '0820'})
res3 = requests.get(url3, params={'getData': 'get'})
res4 = requests.post(url3, data={'postData': 'post'})

print(f'get request response: {res.text}')
print(f'post request response: {res2.text}')
print(f'get/post request response: {res3.text}')
print(f'get/post request response: {res4.text}')
get request response: 999
post request response: today
get/post request response: get
get/post request response: post