简体   繁体   English

如何在 Flask 中通过 Postman 获取 XML 格式的 POST 请求正文作为字典?

[英]How to fetch POST request body in XML format as a dictionary in Flask hit through Postman?

I am new to Flask.我是烧瓶的新手。 How can I fetch POST request body sent in XML format (mimetype="application/xml") through Postman as a dictionary in Flask API?如何通过 Postman 获取以 XML 格式(mimetype="application/xml")发送的 POST 请求正文作为 Flask API 中的字典?

curl --location --request POST 'http://127.0.0.1:5000/test/' \
--header 'Content-Type: application/xml' \
--data-raw '<?xml version="1.0" encoding="UTF-8" ?>
            <root>
                <name>Sam</name>
                <age>22</age>
            </root>'

Is there any method to do so, like we do in case of JSON using:是否有任何方法可以这样做,就像我们在使用 JSON 的情况下所做的那样:

content = flask.request.get_json()

Or is it like that we can only send the POST request body in JSON format?还是只能以JSON格式发送POST请求体?

I have tried xmltodict but I am not able to fetch the XML in Flask through flask.request我尝试过 xmltodict,但我无法通过 flask.request 在 Flask 中获取 XML

content = xmltodict.parse(request.{what??})

Any resources regarding the same will be appreciated.任何与此相关的资源将不胜感激。

You can parse XML, if you send the XML as the value of a key in your POST request, eg POST Data is: xmldata="YOUR XML"您可以解析 XML,如果您将 XML 作为 POST 请求中的键值发送,例如 POST Data 是: xmldata="YOUR XML"

Read for more reference here: Flask Api to parse xml post requests returning errors在此处阅读更多参考: Flask Api to parse xml post requests returns errors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM