简体   繁体   English

Flask 二进制的 Restful reqparse add_argument 类型

[英]Flask Restful reqparse add_argument type for binary

I have created a rest api using Flask Restful and used reqparse to fetch data passed on with POST call.我使用 Flask Restful 创建了 rest api,并使用 reqparse 来获取通过 POST 调用传递的数据。

So far I could fetch data that are passed as raw in Postman.到目前为止,我可以获取在 Postman 中作为原始数据传递的数据。 What should be the type to be included in add_argument of reqparse if I want to use binary in Postman?如果我想在 Postman 中使用二进制,那么 reqparse 的 add_argument 中应该包含什么类型?

Current code:当前代码:

from flask_restful import Resource, reqparse

def post(self):
    parser = reqparse.RequestParser()
    parser.add_argument('dataUrl')
    args = parser.parse_args()

If create parser in next way:如果以下面的方式创建解析器:

upload_parser = server.api.parser()
upload_parser.add_argument('', location='data',
                           type=bytes, required=True)

You will receive error TypeError: a bytes-like object is required, not 'str' I tried to change type= to FileStorage, bytes, bytearray, but nothing helped.您将收到错误 TypeError: a bytes-like object is required, not 'str' 我试图将 type= 更改为 FileStorage, bytes, bytearray,但没有任何帮助。

If instead of data use any location from https://flask-restplus.readthedocs.io/en/stable/_modules/flask_restplus/reqparse.html you will receive an error about missing parameters.如果使用https://flask-restplus.readthedocs.io/en/stable/_modules/flask_restplus/reqparse.html中的任何位置而不是数据,您将收到有关缺少参数的错误。

Use flask_restful.inputs.boolean AND set its default value (otherwise it would not work).使用 flask_restful.inputs.boolean 并设置其默认值(否则它将不起作用)。 Example:例子:

from flask import Flask
from flask_restful import Resource, Api, reqparse, inputs

app = Flask(__name__)
api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument('dataUrl', type=inputs.boolean, default=False)

@app.route('/<string:dataUrl>')
def post(self, dataUrl):
    data = parser.parse_args()
    return data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM