From eebf40cc8242d474f323a7172c05a31702e47d78 Mon Sep 17 00:00:00 2001 From: Johan Lundberg Date: Thu, 10 Jun 2021 15:38:59 +0200 Subject: [PATCH] content-type needs to have json in it to be handled https://github.com/tiangolo/fastapi/pull/2118 --- src/auth_server/middleware.py | 6 ++++++ src/auth_server/tests/test_app.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/auth_server/middleware.py b/src/auth_server/middleware.py index 049a9e7..1b06d9c 100644 --- a/src/auth_server/middleware.py +++ b/src/auth_server/middleware.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- import logging +from typing import Dict from jwcrypto import jws from pydantic import ValidationError +from starlette.datastructures import Headers from starlette.middleware.base import BaseHTTPMiddleware from starlette.requests import Request from starlette.responses import PlainTextResponse @@ -47,6 +49,10 @@ def __init__(self, app): async def dispatch(self, request: Request, call_next): if request.headers.get('content-type') == 'application/jose': + # Return a more helpful error message for a common mistake + return return_error_response(status_code=422, detail='content-type needs to be application/jose+json') + + if request.headers.get('content-type') == 'application/jose+json': config = load_config() request = self.make_context_request(request) logger.info('got application/jose request') diff --git a/src/auth_server/tests/test_app.py b/src/auth_server/tests/test_app.py index 280fedc..f0999e0 100644 --- a/src/auth_server/tests/test_app.py +++ b/src/auth_server/tests/test_app.py @@ -211,7 +211,7 @@ def test_transaction_jws(self): ) data = _jws.serialize(compact=True) - client_header = {'Content-Type': 'application/jose'} + client_header = {'Content-Type': 'application/jose+json'} response = self.client.post("/transaction", data=data, headers=client_header) assert response.status_code == 200