如何在Flask中获取数据形式`request.form`?

时间:2019-10-21 08:32:12

标签: python python-3.x flask

我正在尝试从请求中获取数据,我尝试了以下提到的方法,得到了None{}。 这是我尝试过的,

request.form['text']
request.form.get('text')
request.form.to_dict()
request.form.getlist('text[]')
request.form.getlist('text')

我正在使用小苍蝇(Advanced Rest Client)发送json数据。 这是数据示例,

 [{
    "from": "971559xxxxxx",
    "id": "ABEGkZiAOQVVxxxxxxxxxxxxqEZ7KVtbxxxxx",
    "profile": {
        "name": "John Doe"
    },
    "text": {
        "body": "hi"
    },
    "timestamp": "1562080688",
    "type": "text",
    "to_number": "+971xxxxxxx"
    }]

1 个答案:

答案 0 :(得分:1)

如果它发送json,则可以从其中获取

import html.parser import io from xml.sax.saxutils import XMLGenerator class Reconstructor(html.parser.HTMLParser): def __init__(self): super().__init__() self.op_stream = [] self.tag_stack = [] def handle_startendtag(self, tag, attrs): self.op_stream.append(('startendtag', (tag, attrs))) def handle_starttag(self, tag, attrs): self.op_stream.append(('starttag', (tag, attrs))) self.tag_stack.append(tag) def handle_endtag(self, tag): expected_tag = self.tag_stack[-1] if tag != expected_tag: print('mismatch closing <{}>, expected <{}>'.format(tag, expected_tag)) # TODO: implement logic to figure out the correct order for the tags here # and reorder tag_stack accordingly. stack_tag = self.tag_stack.pop(-1) self.op_stream.append(('endtag', (stack_tag, tag))) def handle_charref(self, name): self.op_stream.append(('charref', (name,))) def handle_entityref(self, name): self.op_stream.append(('entityref', (name,))) def handle_data(self, data): self.op_stream.append(('data', (data,))) def handle_comment(self, data): self.op_stream.append(('comment', (data,))) def handle_decl(self, decl): self.op_stream.append(('decl', (decl,))) def handle_pi(self, data): self.op_stream.append(('pi', (data,))) def generate_xml(self): stream = io.StringIO() xg = XMLGenerator(stream, encoding='utf-8') for op, args in self.op_stream: if op in ('startendtag', 'starttag'): tag, attrib = args xg.startElement(tag, dict(attrib)) if op == 'startendtag': xg.endElement(tag) elif op == 'endtag': tag = args[0] xg.endElement(tag) elif op == 'data': xg.characters(args[0]) else: raise NotImplementedError('Operator not implemented: %s' % op) xg.endDocument() return stream.getvalue() xr = Reconstructor() xr.feed('<trn>choya - <i><com>a cholla cactus </i> lat. <i>Cylindropuntia</com></trn> native to US</i>') y = xr.generate_xml() print(y) print(request.json)