Comments (6)
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']}")
from flask-restx.
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
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.
from flask-restx.
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
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/sendI 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"
from flask-restx.
@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"
}'
from flask-restx.
@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 usingrequest.json
- If you specify in
location=form
, send data in form and receive data as form usingrequest.form
from flask-restx.
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.
from flask-restx.
Related Issues (20)
- Support Werkzeug 3.0 HOT 5
- A way to use custom HTTP methods HOT 2
- Flask Limiter Integration HOT 2
- Provide feedback for improvement
- Update enums schema from OpenAPI 2.0 to OpenAPI 3.0 HOT 2
- Error : `ModuleNotFoundError: No module named 'flask.scaffold'` HOT 2
- How can Model represent a non-json schema for the swagger example?
- [bug] v1.3.0 ModuleNotFoundError: No module named 'importlib.metadata' (v1.2.0 is OK) HOT 1
- request body validation is not working HOT 2
- should RESTX_ERROR_404_HELP be disabled by default? HOT 1
- How to integrate restx with blueprint based library like openbrokerapi
- Problem with adding oauth2.0 (flow=password) authentication function settings on the swagger page
- Get arguments from query string in future versions ? HOT 2
- Inquiry Regarding Organization of CRUD Operations in Flask-RESTx HOT 1
- is it possible to write a custom validation not in business code?
- How do I use async/await with flask-restx? HOT 3
- @api.marshal_with is meant to support headers but gets unexpected keyword argument
- Unit Test "EmailTest.test_invalid_values_check" is failing HOT 1
- Is there a way to sort in the automated documentation? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-restx.