Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request parsing example? #7

Closed
nickretallack opened this issue Dec 4, 2014 · 5 comments
Closed

Request parsing example? #7

nickretallack opened this issue Dec 4, 2014 · 5 comments
Labels

Comments

@nickretallack
Copy link

Is this only for serializing responses, or can it be used for parsing requests as well? Can you add an example of this?

@sloria
Copy link
Member

sloria commented Dec 4, 2014

marshmallow is used for validating/deserializing input data and serializing objects. It does not itself pull values off of flask.request, but marshmallow pairs nicely with a library like webargs.

I am planning on writing up--either in an example in the marshmallow repo or in a blog post--how to use marshmallow and webargs together.

@sloria sloria added the question label Dec 7, 2014
@ghost
Copy link

ghost commented Dec 22, 2014

I'm having issues with creating an object with a Hyperlinks attribute
I get AttributeError: 'Hyperlinks' object has no attribute 'attribute'; I assume this is is because it's trying to make a Hyperlinks object although it should be auto-generated like marshmallow.URL. Any ideas?

@sloria
Copy link
Member

sloria commented Dec 22, 2014

@raj-kesavan Can you please provide a reproducible example to help debug your problem?

@ghost
Copy link

ghost commented Dec 22, 2014

@sloria
Simple set up with Flask-RESTful and Flask-Marshmallow. When I navigate to /api/v1/person, I get a 500 Internal Server Error. Server logs at bottom.

class PersonSchema(ma.Schema):
    name = ma.String(required = True)
    friends = ma.Hyperlinks({
        'href': ma.AbsoluteURL('person_ep')
    })

class PersonAPI(Resource):
    def get(self):
        person, errors = PersonSchema().load({
            'name': 'Rob'
        })

api.add_resource(PersonAPI, '/api/v1/person/', endpoint = 'person_ep')

Server log

[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 521, in dispatch_request
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     resp = meth(*args, **kwargs)
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/var/www/html/devabacus/abacus/views.py", line 95, in get
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     'name': 'Rob'
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/marshmallow/schema.py", line 498, in load
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     dict_class=self.dict_class
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/marshmallow/fields.py", line 260, in deserialize
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     key = fields_dict[attr_name].attribute or attr_name
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69] AttributeError: 'Hyperlinks' object has no attribute 'attribute'

@sloria
Copy link
Member

sloria commented Dec 22, 2014

@raj-kesavan I see. Thank you for the clarification. I will work on a fix for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants