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

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

Open sandyboxy opened this issue 2 years ago • 6 comments

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",
}'

sandyboxy avatar Feb 07 '24 15:02 sandyboxy

Flask puts form data in a different place in the Request object. You need to call request.form to get form data directly. As you see, request.json will be empty or not set in the case that only form data is passed.

However, you have set up a request parser here but you don't currently use it. So you should be able to use it to retrieve the values regardless of location passed to your code. Something like:

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):
        input_args = parser.parse_args() # This returns a dictionary containing the parsed values from the specified locations.
        print(f"Title: {input_args['title']}")
        print(f"Content: {input_args['content']}")

peter-doggart avatar Feb 07 '24 15:02 peter-doggart

So, if I use location=('form', 'json')

  • a POST request with empty data is call (see picture below);
  • params are passed in URL request instead of body

flask

This is the output I wish to have:

CURL

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

Request URL

http://localhost:5000/send

I know I can have this by set location='json' but, as I said, in this way I would not have a form with input fields.

sandyboxy avatar Feb 08 '24 07:02 sandyboxy

So, if I use location=('form', 'json')

  • a POST request with empty data is call (see picture below);
  • params are passed in URL request instead of body

flask

This is the output I wish to have:

CURL

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

Request URL

http://localhost:5000/send

I know I can have this by set location='json' but, as I said, in this way I would not have a form with input fields.

@sandyboxy, try with location="form"

kartikeyporwal avatar Feb 09 '24 12:02 kartikeyporwal

@sandyboxy, try with location="form"

Hi @kartikeyporwal, if I try with location="form" setting I obtain the following output:

curl -X 'POST' \
  'http://localhost:5000/send' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'title=test&content=test'

instead of having the following:

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

sandyboxy avatar Feb 09 '24 13:02 sandyboxy

@sandyboxy, try with location="form"

Hi @kartikeyporwal, if I try with location="form" setting I obtain the following output:

curl -X 'POST' \
  'http://localhost:5000/send' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'title=test&content=test'

instead of having the following:

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

Why you need the json when you are specifying the params as form? If you need to pass json then specify location="json".

  • If you specify in location=json, send data in json and receive data as json using request.json
  • If you specify in location=form, send data in form and receive data as form using request.form

kartikeyporwal avatar Feb 13 '24 11:02 kartikeyporwal

Simply I wish to use swagger/flask-api to show a simplified page to test API (using form view) and show output as json output instead of form.

sandyboxy avatar Feb 13 '24 12:02 sandyboxy