RequestParser:无法分析字典参数

2024-09-30 04:32:37 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试将dictionary对象传入请求时,RequestParser抛出400错误,并使用{error\u msg}返回以下错误:

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正常工作,但当以编程方式执行时,它不起作用。那么,为什么会发生此错误呢

如果我遗漏了什么信息,请告诉我


Tags: pathnametestparserlabelsdictionaryis错误
1条回答
网友
1楼 · 发布于 2024-09-30 04:32:37

如果希望参数采用字典,则需要通过json传递数据

toUpdateWith = {"name": "Test", "description": "This is a test."}
toUpdateWith['labels'] = { "4" : "create"}
response = self.conn.post('/path/2', json=toUpdateWith)

相关问题 更多 >

    热门问题