pythonflaskmicrosoft-graph-apimime-typesflask-restful

Flask-restful: Did not attempt to load JSON data because the request Content-Type was not 'application/json'


Flask==2.3.3
Werkzeug==2.3.7

I'm trying to get a ms graph subscription going in my app but when I send the subscription request to:

https://graph.microsoft.com/v1.0/subscriptions

and get the follow-up request from ms grap to my endpoint (ValidateSubscription), flask refuses to parse it because of its incorrect mediatype which is text/plain. So far I've tried using flask-accept module to parse the response like this:

class ValidateSubscription(Resource):
    @accept('text/plain')
    def post:
        if flask.request.args.get("validationToken"):
            token = flask.request.args.get('validationToken')
            return Response(status=200, mimetype='text/plain', response=token)
        else:
            # process notification
            pass

but it didn't work and I got the same error.

Also I've tried to add an api representation to my flask app like this:

@api.representation('text/plain')
def output_text(data, code, headers=None):
    resp = flask.make_response(data, code, headers)
    resp.headers.extend(headers or {})
    return resp

When I print out api.representations I see:

OrderedDict([('application/json', <function output_json at 0x7f872b021424>), ('text/plain', <function output_text at 0x7f8728d04214>)])

And I still get the same exact error without any change whatsoever. Is there a better way to allow flask-restful to accept a text/plain header or am I doing something wrong?


Solution

  • So the only solution I found working is rolling flask and Werkzeug back to versions 2.0.0.

    This is due to some commit in newer Werkzeug version that instead of sometimes throwing a, quote, "Poorly comprehendable error message" when Content-Type of the incoming request isn't application/json, simply throws 415 INCORRECT MEDIA TYPE error everytime this happens.