flask-restplus icon indicating copy to clipboard operation
flask-restplus copied to clipboard

Can I Post a json?

Open wangshunping opened this issue 9 years ago • 4 comments
trafficstars

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')


wangshunping avatar Jan 06 '16 09:01 wangshunping

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()

harishkashyap avatar Jan 29 '16 01:01 harishkashyap

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()

maxkoryukov avatar Mar 17 '17 13:03 maxkoryukov

@maxkoryukov

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

xbritbx avatar Feb 11 '20 21:02 xbritbx

@maxkoryukov

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

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

louq-gharnati avatar Dec 25 '20 19:12 louq-gharnati