栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有POST的flask示例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有POST的flask示例

在实际回答你的问题之前:

URL

key=listOfUsers/user1
中的GET参数(例如)是参数,你不应将其用于
POST
请求。关于GET和POST之间的区别的快速说明可以在这里找到。

就你而言,要利用REST原理,你可能应该具有:

http://ip:5000/usershttp://ip:5000/users/<user_id>

然后,在每个网址,你可以定义不同的HTTP方法的行为(GET,POST,PUT,DELETe)。例如,在上/users/,你需要以下内容:

GET /users/<user_id> - return the information for <user_id>POST /users/<user_id> - modify/update the information for <user_id> by providing the dataPUT - I will omit this for now as it is similar enough to `POST` at this level of depthDELETE /users/<user_id> - delete user with ID <user_id> 

所以,在你的榜样,你想要做一个

POST
/users/user_1
与POST数据是
"John"
。然后,应该向用户隐藏XPath表达式或你要访问数据的任何其他方式,并且不要与URL紧密耦合。这样,如果你决定更改存储和访问数据的方式,而不是更改所有URL,则只需更改服务器端的代码即可。

现在,你的问题的答案:下面是基本的半伪代码,说明如何实现我上面提到的内容:

from flask import Flaskfrom flask import requestapp = Flask(__name__)@app.route('/users/<user_id>', methods = ['GET', 'POST', 'DELETE'])def user(user_id):    if request.method == 'GET':        """return the information for <user_id>"""        .        .        .    if request.method == 'POST':        """modify/update the information for <user_id>"""        # you can use <user_id>, which is a str but could        # changed to be int or whatever you want, along        # with your lxml knowledge to make the required        # changes        data = request.form # a multidict containing POST data        .        .        .    if request.method == 'DELETE':        """delete user with ID <user_id>"""        .        .        .    else:        # POST Error 405 Method Not Allowed        .        .        .

还有很多其他事情需要考虑,例如POST请求内容类型,但我认为到目前为止我所说的应该是一个合理的起点。我知道我没有直接回答你所问的确切问题,但希望对你有所帮助。我稍后也会进行一些编辑/添加。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381127.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号