Rob
Rob

Reputation: 3459

parsing the Accept Header in flask-restful for API versioning

Say I have an accept header like:

'Accept: application/vnd.myapp.login.v2+json'

and this is sent to the route:

/login

how would I parse this in flask to get the version number? I'd assume something like:

if request.accept_mimetypes == 'application/vnd.myapp.login.v2+json':
    # use logic for v2
    pass
else:
    # use default logic
    pass

But I'd imagine there was a cleaner way to parse this string to ensure the vendor and version are properly specified.

Upvotes: 1

Views: 1137

Answers (1)

tanglong
tanglong

Reputation: 278

As flask.Request.headers mentioned:

The incoming request headers as a dictionary like object.

It maybe look like this:

if request.headers['Accept'] == 'application/vnd.myapp.login.v2+json':
    pass

Upvotes: 2

Related Questions