noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 506 forks source link

Can I Post a json? #116

Open wangshunping opened 8 years ago

wangshunping commented 8 years ago

as I see in a demo from swagger.io, i could post a json file image

flask-restplus support it ?

can can i modify argument ?

I modify parser this way and it doesn't work.


parser = api.parser()
parser.add_argument('body', type=json, required=True, help='json content', location='json')
harishkashyap commented 8 years ago

just do api.route over your class instead of type=json

class Test(fields.Raw): def format(self, value): return {'x': ['whatever']}

test_fields = api.model('Test', { 'a': fields.String(required=True, description='...', example='an example'), 'b': Test(example="hey") })

use @api.route('/whatever', methods=[post]) class whatever(Resource): def post(self): @api.expect(fields=test_fields) all_params = request.json()

maxkoryukov commented 7 years ago

it is a formatted copy of @harishkashyap 's comment:

just do api.route over your class instead of type=json


class Test(fields.Raw):
  def format(self, value):
    return {'x': ['whatever']}

test_fields = api.model('Test', {
  'a': fields.String(required=True, description='...', example='an example'),
  'b': Test(example="hey")
})

# use 
@api.route('/whatever', methods=["post"])
class whatever(Resource):
  def post(self):
    @api.expect(fields=test_fields)
    all_params = request.json()
xbritbx commented 4 years ago

@maxkoryukov

This helped, except I needed to use request.json for it to work

louq-gharnati commented 3 years ago

@maxkoryukov

This helped, except I needed to use request.json for it to work

Could you explain how you solved it with request.json?