与POST的Flask示例 [英] Flask example with POST

查看:120
本文介绍了与POST的Flask示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下路线访问xml文件,用特定的xpath(?key =)替换特定标签的文本:

  def update_text():
#CODE
pre>

然后,我将使用如下所示的cURL:
$ b $ $ $ p $ curl -X POST http:// ip:5000 / resource?key = listOfUsers / user1 -dJohn

xpath表达式 listOfUsers / user1 应该访问标签< user1> 来将其当前文本改变为John 。

我不知道如何做到这一点,因为我刚刚开始学习Flask和REST,我找不到任何这个具体案例的好例子。另外,我想使用lxml来操纵xml文件,因为我已经知道了。



有人可以帮忙,并提供一个例子来指导我吗?

解决方案

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

URL中的参数(例如 key = listOfUsers / user1 GET 参数,你不应该在 POST 请求中使用它们。 GET和POST之间的差异的快速解释可以发现在这里



在您的情况下,要使用REST原则,您应该有:

  http:// ip:5000 / users 
http:// ip:5000 / users /< user_id>然后,在每个URL上,你可以定义不同HTTP方法的行为(

>

> GET POST PUT DELETE )。例如,在 / users /< user_id> 中,您需要以下内容:

  GET / users /< user_id> - 返回< user_id>的信息
POST / users /< user_id> - 修改/更新< user_id>的信息通过提供数据
PUT - 我现在将省略这个,因为它与在这个深度级别的$ POST相似,足够类似于
DELETE / users /< user_id> - 删除ID为< user_id>的用户

所以,在你的例子中,你需要做一个 POST / users / user_1 ,POST数据为John。然后,XPath表达式或任何其他想要访问数据的方式都应该隐藏起来,而不是与URL紧密结合。这样,如果你决定改变你存储和访问数据的方式,而不是所有的URL改变,你只需要改变服务器端的代码。



<现在,您的问题的答案:
下面是如何实现我上面提到的一个基本的半伪代码:

  @ app.route('/ users /< user_id>',methods = ['GET','POST','DELETE'])
def user user_id):
if request.method =='GET':
返回< user_id>
的信息。


if request.method =='POST':
修改/更新< user_id>的信息
#您可以使用< user_id> ;,这是一个str但可以
#更改为int或任何你想要的,沿
#与你的lxml的知识,使所需的
#变化
数据= request.form#一个multidict包含发布数据



if request.method =='DELETE':
删除ID为user_id>
的用户。


else:
#POST错误405方法不允许



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



谢谢,我希望这会有所帮助。如果我弄错了,请告诉我。

Suppose the following route which accesses an xml file to replace the text of a specific tag with a given xpath (?key=):

@app.route('/resource', methods = ['POST'])
def update_text():
    # CODE

Then, I would use cURL like this:

curl -X POST http://ip:5000/resource?key=listOfUsers/user1 -d "John"

The xpath expreesion listOfUsers/user1 should access the tag <user1> to change its current text to "John".

I have no idea on how to achieve this because I'm just starting to learn Flask and REST and I can't find any good example for this specific case. Also, I'd like to use lxml to manipulate the xml file since I already know it.

Could somebody help and provide an example to guide me?

解决方案

Before actually answering your question:

Parameters in a URL (e.g. key=listOfUsers/user1) are GET parameters and you shouldn't be using them for POST requests. A quick explanation of the difference between GET and POST can be found here.

In your case, to make use of REST principles, you should probably have:

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

Then, on each URL, you can define the behaviour of different HTTP methods (GET, POST, PUT, DELETE). For example, on /users/<user_id>, you want the following:

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

So, in your example, you want do a POST to /users/user_1 with the POST data being "John". Then the XPath expression or whatever other way you want to access your data should be hidden from the user and not tightly couple to the URL. This way, if you decide to change the way you store and access data, instead of all your URL's changing, you will simply have to change the code on the server-side.

Now, the answer to your question: Below is a basic semi-pseudocode of how you can achieve what I mentioned above:

@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
    .
    .
    .

There are a lot of other things to consider like the POST request content-type but I think what I've said so far should be a reasonable starting point. I know I haven't directly answered the exact question you were asking but I hope this helps you. I will make some edits/additions later as well.

Thanks and I hope this is helpful. Please do let me know if I have gotten something wrong.

这篇关于与POST的Flask示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆