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?
Just put double quote in URL, like this:
Before actually answering your question:
Parameters in a URL (e.g.
key=listOfUsers/user1
) areGET
parameters and you shouldn't be using them forPOST
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:
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: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:
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.
Here is the example in which you can easily find the way to use Post,GET method and use the same way to add other curd operations as well..