Skip to main content

Error reported when using flask_restful.reqparse.parse_args: 415 Unsupported Media Type

flask code

class UserList(Resource):
# Process get requests
def get(self):
# Create a parsing object through RequestParser
parser = reqparse.RequestParser()
# By parsing the add of an object_ Argument() adds validation fields,
# The location=['args'] here is crucial, and the default is ['json '], which is suitable for the data requested by the front-end in JSON format.
# Because it is currently a get request, use ['args'] to receive the query string parameters of the front-end request
parser.add_argument('name', type=str, location=['args'])
try:
# Use parse to parse objects_Args() for verification,
# If correct, return the validated and qualified parameter values. If incorrect, throw an exception to the client
args = parser.parse_args()
return {'msg': 'Data validation successful'}
except Exception as e:
print(e)
return {"message": e}

postman

postman carries query parameters:?name=zs

Backend error

415 Unsupported Media Type: Did not attempt to load JSON data because the request Content-Type was not 'application/json'.

415 Unsupported Media Type: No attempt was made to load JSON data because the requested content type was not 'application/JSON'.

Error analysis

The data type requested by postman is inconsistent with the type that the flask backend wants to receive.

Solution:

In the postman request header ( Headers) add:Content-Type: application/json

Backend error

400 Bad Request: Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)

Error analysis

It probably means that data that conforms to the type is not passed.

Solution:

Select postmanand in and add a . Of course, if the query parameters are not passed , they can also be given inside.Body row JSON(application/json){}

?name=zs{}