Skip to content

How to use 'form' location and process request as json? #594

Open
@sandyboxy

Description

@sandyboxy

Ask a question
How to use 'form' location and process request as json?

Additional context
I wish to use form field to allow users to test my API by filling field instead of to fill a raw JSON dict.
This is my relevant code:

parser = api.parser()
parser.add_argument('title', type=str, help='Some param', required=True, location=('form', 'json'))
parser.add_argument('content', type=str, help='Some param',  required=True, location=('form', 'json'))

@api.route('/send')
class Send(Resource):
    @api.doc(parser=parser, validate=True)
    def post(self, **kwargs):
        print(request.args.get('title'))   # it prints title
        print(request.json) # it fails

when I set field location='form' I have the form with fields to fill but the request is

curl -X 'POST' \
  'http://localhost:5000/send?title=test&content=test' \
  -H 'accept: application/json' \
  -d ''

but I wish to have the following output without to set location='json'

curl -X 'POST' \
  'http://localhost:5000/send' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "title",
  "content": "content",
}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions