Reputation: 351
I have a simple flask server (with no sqlalchemy at this stage) and it is unable to parse request for a mandatory field
Server python code -->
from flask import request, jasonify, make_response, abort
from marshmallow import Schema, fields
class MySchema(Schema):
ref_date=fields.Str(required=True)
schema_instance = MySchema()
@app.route('/functions/regression',method=['GET'])
def perform_regression():
print('ref_date: ', request.args.get('ref_date'))
errors = schema_instance.validate(request.form)
if errors:
abort(400, str(errors))
app.run()
I have a client python code as follows -->
import requests
res = requests.get("http://127.0.0.1:5000/functions/regression?ref_date='20200205'")
print(res.status_code, res.text)
I am always getting error message "Missing data for required field".
Any pointers? I do get the parameters correctly on the server as can be seen with request.args.get()
but after this Schema returns the error.
Upvotes: 0
Views: 4176
Reputation: 351
Based on suggestion of Igor, the following works
from flask import request, jasonify, make_response, abort
from marshmallow import Schema, fields
class MySchema(Schema):
ref_date=fields.Str(required=True)
schema_instance = MySchema()
@app.route('/functions/regression',method=['GET'])
def perform_regression():
print('ref_date: ', request.args.get('ref_date'))
errors = schema_instance.validate(request.args) #<--fix here
if errors:
abort(400, str(errors))
app.run()
Upvotes: 1
Reputation: 5611
Serializer expects your data to be in request payload, not in request parameters.
So, you have two options:
{"ref_date": "20200205"}
)request.form
.Basically, the problem is that request.form
has no data, so serializer throws error about missing required field.
Upvotes: 0