Add middleware to normalize Content-Type header#2334
Add middleware to normalize Content-Type header#2334utafrali wants to merge 1 commit intogithub:mainfrom
Conversation
Strip MIME parameters like charset from Content-Type so handlers don't reject requests with charset=utf-8 and similar.
|
Thanks for looking into this! I wanted to flag that the go-sdk already handles mediaType, _, err := mime.ParseMediaType(req.Header.Get("Content-Type"))
if err != nil || mediaType != "application/json" {
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
return
}This Content-Type validation was added in go-sdk v1.4.1 as part of cross-origin protection. The current OSS repo uses go-sdk v1.3.1-pre which predates this check entirely (so there's no Content-Type validation to normalize for). When the go-sdk dependency is updated, @utafrali — are you currently able to reproduce this issue? If so, could you share what endpoint you're hitting and the exact error response? That would help us understand if there's something else going on beyond the Content-Type header parsing. |
Fixes #2333
Summary
Added middleware that normalizes the Content-Type header by stripping MIME parameters like charset.
Why
The API was rejecting requests with
Content-Type: application/json; charset=utf-8because downstream validation expected exactlyapplication/json. This middleware strips those extra parameters so handlers see the clean media type.What changed
Created a new middleware that uses mime.ParseMediaType to extract just the media type, then rewrites the header if parameters were present. Registered it early in the handler's middleware chain so all downstream handlers get the normalized version. Included tests covering cases with no parameters, charset parameter, multiple parameters, and empty headers.
MCP impact
MCP clients that send Content-Type headers with charset parameters will now work correctly. The normalized header is transparent to downstream handlers and doesn't change behavior for anything else.
Prompts tested
N/A
Security/limits consideration
No security implications. The middleware only parses and normalizes the header, it doesn't validate or reject anything.
Tool renaming considerations
N/A
Checklist
./script/lint./script/test