简体   繁体   中英

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.

So far I could fetch data that are passed as raw in Postman. What should be the type to be included in add_argument of reqparse if I want to use binary in Postman?

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.

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.

Use flask_restful.inputs.boolean AND set its default value (otherwise it would not work). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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