Using flask-restful
I'm trying to capture a JSON array passed into the body of a POST request, but can't seem to find it using the RequestParser
, any ideas?
Currently my code looks like this:
# api.py
from flask import Flask
from flask_restful import Api
from resources import CustomRange
app = Flask(__name__)
api = Api(app)
api.add_resource(CustomRange, '/customrange/<start>/<end>')
app.run()
# resources.py
from flask_restful import Resource, reqparse
parser = reqparse.RequestParser()
parser.add_argument('data', action="append")
#parser.add_argument('data', type=int, location="json")
#parser.add_argument('data', type=int, location="form")
#parser.add_argument('data', location="json")
#parser.add_argument('data', location="json", action="append")
#parser.add_argument('data', type=list)
#parser.add_argument('data', type=list, location="json")
#parser.add_argument('data', type=list, location="form")
## None of the above variants seem to capture it
class CustomRange(Resource):
def post(self, start: int, end: int):
args = parser.parse_args()
return args # Always {'data': None}
I tried a simple example of passing the following into the POST body:
[1, 2, 3]
But no dice. Even tried without parser.add_argument
.
Here's the full test request that I'm using:
curl --location --request POST '127.0.0.1:5000/customrange/1/100' \
--header 'Content-Type: application/json' \
--data-raw '[1, 2, 3]'
Currently reqparse
will handle only JSON objects as body (source: will fail on any other object except dict and MultiDict), and not any other type. You'll have to grab the request
object directly for your needs.
from flask import request
from flask_restful import Resource
class CustomRange(Resource):
def post(self, start: int, end: int):
args = request.json
# rest of your code