how to make parse of list(string) not list(char) in parse argument of list?

情到浓时终转凉″ 提交于 2019-12-11 06:01:02

问题


I use flask_restful in flask

My code like:

from flask_restful import Resource, reqparse

apilink_parser = reqparse.RequestParser()
apilink_parser.add_argument('provider_id', type=int,required=True)
apilink_parser.add_argument('name', type=str, required=True)
apilink_parser.add_argument('func_id', type=int)
apilink_parser.add_argument('method', type=str)
apilink_parser.add_argument('url', type=str)
apilink_parser.add_argument('parameter',type=list)
apilink_parser.add_argument("expectreturn", type=list)


@marshal_with(apilink_fields)
def post(self):
    args = apilink_parser.parse_args()
    print(args)
    # user owns the task
    task = APILink.create(**args)
    return task, 201

My json post data like:

{ 
"name":"riskiqwhois",
"provider_id":1,
"func_id":1,
"url":"myurl",
"parameter":["query"],  //******//
"expectreturn":[],
"method":"post"
 }

but when I print the arrgs the result is:

 {
 'provider_id': 1, 
 'name': 'riskiqwhois', 
 'func_id': 1, 
 'method': 'post', 
 'url': 'myurl', 
 'parameter': ['q', 'u', 'e', 'r', 'y'], //******//
 'expectreturn': None
  }

I want You can see I want parameter is list of string which is only one element named "query", but the real parameter tranlate into the database is ['q', 'u', 'e', 'r', 'y'], How to make the parameter is list of string not list of char? how to make sure the data is list(string)?


回答1:


You can solve this problem by adding action="append" to your request parser like below

apilink_parser.add_argument('parameter',type=str,action="append")
apilink_parser.add_argument("expectreturn", type=list,action="append")

this will return you below output

 {
 'provider_id': 1, 
 'name': 'riskiqwhois', 
 'func_id': 1, 
 'method': 'post', 
 'url': 'myurl', 
 'parameter': ['query'],
 'expectreturn': None
  }



回答2:


I think the reason is that you didn't set location(json). Example:

apilink_parser.add_argument('parameter', type=list, location='json')

Also make sure that you sent header Content-Type: application/json

Hope this helps.



来源:https://stackoverflow.com/questions/48864939/how-to-make-parse-of-liststring-not-listchar-in-parse-argument-of-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!