RequestParser.parse_args()无法识别参数

时间:2020-07-18 16:47:58

标签: python api rest put flask-restful

我正在使用flask_restful创建REST API,并想解析来自PUT请求的参数。下面是相关代码:

user_put_args = reqparse.RequestParser()
user_put_args.add_argument("user_id", type="str", help="user_id is needed for this user")
user_put_args.add_argument("spotify_username", type="str", help="Spotify username for the corresponding user_id")
user_put_args.add_argument("apple_music_username", type="str", help="Apple Music username for the corresponding user_id")

PUT函数声明:

def put(self, user_id):
   args = user_put_args.parse_args()
   rest of code ...

这是放置请求:

req4 = requests.put(HEROKU_BASE + "/users/" + "test1", {"spotify_username": "test_1", "apple_music_username":"something"})
print(req4.json())

我从看涨期权获得的响应是​​:

{'message': {'spotify_username': 'Spotify username for the corresponding user_id'}}

表示它正挂在请求解析上。

关于如何解决此问题的任何想法?我遵循的指南将其引向了这一点,并且我的代码几乎完全相同。谢谢!

1 个答案:

答案 0 :(得分:0)

在编写type="str"时,reqparser尝试在数据上调用type的自变量,而不能用字符串来完成
您不应该在此处使用引号,而只是这样:
user_put_args.add_argument("spotify_username", type=str, help="Spotify username for the corresponding user_id")

相关问题