RequestParser:无法解析字典参数
RequestParser: Cannot Parse Dictionary Argument
当尝试将字典对象传递到请求中时,RequestParser
抛出 400 错误,并且使用 {error_msg}", returns 出现以下错误:
dictionary update sequence element #0 has length 1; 2 is required
我尝试解析参数的方式如下:
patch_parser = reqparse.RequestParser()
# ...
patch_parser.add_argument("labels", type=dict, help="{error_msg}", store_missing=False)
# some POST/PUT request function
@app.route('/path/<id:int>')
def someFunction():
args = patch_parser.parse_args()
# ...
导致请求的代码:
toUpdateWith = {"name": "Test", "description": "This is a test."}
toUpdateWith['labels'] = { "4" : "create"}
response = self.conn.post('/path/2', data=toUpdateWith)
传入的数据为:
{
"name": "Test",
"description": "This is a test.",
"labels": {
"4": "create"
}
}
虽然这可以通过 Postman 正常工作,但在以编程方式执行时不起作用。那么,为什么会出现此错误?
如果我遗漏了任何信息,请告诉我。
如果您想要一个接受字典的参数,则需要通过 json 传递数据。
toUpdateWith = {"name": "Test", "description": "This is a test."}
toUpdateWith['labels'] = { "4" : "create"}
response = self.conn.post('/path/2', json=toUpdateWith)
当尝试将字典对象传递到请求中时,RequestParser
抛出 400 错误,并且使用 {error_msg}", returns 出现以下错误:
dictionary update sequence element #0 has length 1; 2 is required
我尝试解析参数的方式如下:
patch_parser = reqparse.RequestParser()
# ...
patch_parser.add_argument("labels", type=dict, help="{error_msg}", store_missing=False)
# some POST/PUT request function
@app.route('/path/<id:int>')
def someFunction():
args = patch_parser.parse_args()
# ...
导致请求的代码:
toUpdateWith = {"name": "Test", "description": "This is a test."}
toUpdateWith['labels'] = { "4" : "create"}
response = self.conn.post('/path/2', data=toUpdateWith)
传入的数据为:
{
"name": "Test",
"description": "This is a test.",
"labels": {
"4": "create"
}
}
虽然这可以通过 Postman 正常工作,但在以编程方式执行时不起作用。那么,为什么会出现此错误?
如果我遗漏了任何信息,请告诉我。
如果您想要一个接受字典的参数,则需要通过 json 传递数据。
toUpdateWith = {"name": "Test", "description": "This is a test."}
toUpdateWith['labels'] = { "4" : "create"}
response = self.conn.post('/path/2', json=toUpdateWith)