6

I have a flask_restful API set up but want users to pass numpy arrays when making a post call. I first tried using reqparse from flask_restful but it doesn't support data types of numpy.array. I am trying to use marshmallow (flask_marshmallow) but am having trouble understanding how the arguments get passed to the API.

Before I was able to do something like:

from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('url', type=str)
parser.add_argument('id', type=str, required=True)

But now I am not sure how to translate that into marshmallow. I have this set up:

from flask_marshmallow import Marshmallow
app = Flask(__name__)
ma = Marshmallow(app)
class MySchema(ma.Schema):
    id = fields.String(required=True)
    url = fields.String()

But how can I load in what a user passes in when making a call by using my newly defined schema?

ladderfall
  • 145
  • 6

1 Answers1

0

I don't know flask-marshmallow and the docs only show how to use it to serialize responses.

I guess to deserialize requests, you need to use webargs, developped and maintained by the marshmallow team.

It has marshmallow integration:

from marshmallow import Schema, fields
from webargs.flaskparser import use_args


class UserSchema(Schema):
    id = fields.Int(dump_only=True)  # read-only (won't be parsed by webargs)
    username = fields.Str(required=True)
    password = fields.Str(load_only=True)  # write-only
    first_name = fields.Str(missing="")
    last_name = fields.Str(missing="")
    date_registered = fields.DateTime(dump_only=True)


@use_kwargs(UserSchema())
def profile_update(username, password, first_name, last_name):
    update_profile(username, password, first_name, last_name)
    # ...
Jérôme
  • 13,328
  • 7
  • 56
  • 106