flask参数传递的几种方式,flask 参数传递详解

flask 参数传递有几种方式

flask作为python中web开发的佼佼者框架,使用非常的便捷,今天何三就跟大家聊聊flask参数传递的几种常见方式。

flask参数传递常用方式如下:

  1. Get方式传参
  2. Post方式传参
  3. url路径传参
  4. json body 传参 ...

1、Get方式传参

代码:

from flask import Flask, request, jsonify
import json

app = Flask(__name__)

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

# get方式传参数
@app.route('/name')
def getName():
    name = request.args.get('name','')
    return 'hello,{}'.format(name)

测试

$ curl -i -X GET http://127.0.0.1:5000/name?name=www.h3blog.com
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    20  100    20    0     0   6666      0 --:--:-- --:--:-- --:--:-- 10000HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 20
Server: Werkzeug/1.0.1 Python/3.8.0
Date: Mon, 11 May 2020 08:17:42 GMT

hello,www.h3blog.com

2、Post方式传参

代码

from flask import Flask, request, jsonify
import json

app = Flask(__name__)

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

# post方式传参数
@app.route('/name',methods=['POST'])
def postName():
    name = request.form.get('name','')
    return 'hello,{}'.format(name)

测试

$ curl -X POST http://127.0.0.1:5000/name -d 'name=www.h3blog.com' -i             % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    39  100    20  100    19    625    593 --:--:-- --:--:-- --:--:--  1258HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 20
Server: Werkzeug/1.0.1 Python/3.8.0
Date: Mon, 11 May 2020 08:19:27 GMT

hello,www.h3blog.com

3、url路径传参

代码

from flask import Flask, request, jsonify
import json

app = Flask(__name__)

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

# url路径参数
@app.route('/name/<name>')
def urlName(name):
    return 'hello,{}'.format(name)

测试

$ curl -X GET http://127.0.0.1:5000/name/www.h3blog.com -i
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    20  100    20    0     0  10000      0 --:--:-- --:--:-- --:--:-- 20000HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 20
Server: Werkzeug/1.0.1 Python/3.8.0
Date: Mon, 11 May 2020 08:21:12 GMT

hello,www.h3blog.com

4、json body 传参

代码

from flask import Flask, request, jsonify
import json

app = Flask(__name__)

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


# ajax body json
@app.route('/json',methods=['POST'])
def json_body():
    data = request.get_data(as_text=True)
    print(data)
    jsonObj = json.loads(data)
    return jsonify(jsonObj)

测试

$ curl -X POST -H 'Content-Type:application/json' http://127.0.0.1:5000/json -d '{"name":"www.h3blog.com"}' -i
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    31  100    25  10333   8333 --:--:-- --:--:-- --:--:-- 28000HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 31
Server: Werkzeug/1.0.1 Python/3.8.0
Date: Mon, 11 May 2020 08:22:08 GMT

{
  "name": "www.h3blog.com"
}

flask传递参数整体代码

from flask import Flask, request, jsonify
import json

app = Flask(__name__)

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

# get方式传参数
@app.route('/name')
def getName():
    name = request.args.get('name','')
    return 'hello,{}'.format(name)

# post方式传参数
@app.route('/name',methods=['POST'])
def postName():
    name = request.form.get('name','')
    return 'hello,{}'.format(name)

# url路径参数
@app.route('/name/<name>')
def urlName(name):
    return 'hello,{}'.format(name)

# ajax body json
@app.route('/json',methods=['POST'])
def json_body():
    data = request.get_data(as_text=True)
    print(data)
    jsonObj = json.loads(data)
    return jsonify(jsonObj)

总结

flask接收参数还是比较简单的,以上只是对flask参数传递的几点总结,当然还有很多的其他的传参方式,我这里就不一一例举了