Flask-Restful:不能解析json嵌套值的args

时间:2018-03-01 08:42:03

标签: flask-restful flask-restplus

我有json

{
    "message":  {
        "foo": "foo",
        "bar": "bar"
    }
}

解析器:

parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument('foo', type=str, required=True)
parser.add_argument('bar', type=str, required=True)
args = parser.parse_args()

错误是: {' foo':'在JSON正文或帖子正文或查询字符串中缺少必需参数',' bar':'缺少必需参数in JSON正文或帖子正文或查询字符串'}

1 个答案:

答案 0 :(得分:0)

由于您的'foo''bar'密钥位于'message'内,因此JSON解析必须从“message”开始。 即。在从'message'解析'foo'之前,您必须先了解解析器'message'

为此,您必须设置一个解析您的root_parser = reqparse.RequestParser() root_parser.add_argument('message', type=dict) root_args = root_parser.parse_args() message_parser = reqparse.RequestParser() message_parser.add_argument('foo', type=dict, location=('message',)) message_parser = message_parser.parse_args(req=root_args) 的根解析器。您可以通过以下方式执行此操作:

    MyMessage.From = New Net.Mail.MailAddress(mEmailAddress)
    MyMessage.To.Add(mToAddress)
    MyMessage.Subject = mSubject
    MyMessage.Body = mBody

    Smtp.Host = "------"
    Smtp.Port = "2525"
    Smtp.Credentials = New NetworkCredential(------)

    Smtp.Send(MyMessage)        ' Actual Send

    Dim oldCDO As CDO.Message
    oldCDO = MyLoadEmlFromFile("template.eml")  ' just put from, to, subject blank. leave first line blank
    oldCDO.To = mToAddress
    oldCDO.From = mEmailAddress
    oldCDO.Subject = mSubject
    oldCDO.TextBody = mBody
    oldCDO.HTMLBody = mBody
    oldCDO.GetStream.Flush()
    oldCDO.GetStream.SaveToFile(yourPath)

有关详细信息,请查看issue from github