Skip to content

Commit ee5e3d2

Browse files
committed
fix: remove Content-Type header from GET requests in OAuth metadata discovery
GET requests to /.well-known/oauth-authorization-server should not include a Content-Type header. Some authorization servers (e.g. Keycloak) respond with 415 Unsupported Media Type when a GET request carries Content-Type: application/json, breaking OAuth metadata discovery. Move Content-Type assignment inside the custom fetch wrapper so it is only applied when the request carries a body (i.e. POST), leaving GET requests header-clean. Fixes #1143
1 parent 7c8b031 commit ee5e3d2

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

client/src/lib/hooks/useConnection.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,16 +582,22 @@ export function useConnection({
582582
switch (transportType) {
583583
case "sse":
584584
requestHeaders["Accept"] = "text/event-stream";
585-
requestHeaders["content-type"] = "application/json";
586585
transportOptions = {
587586
authProvider: serverAuthProvider,
588587
fetch: async (
589588
url: string | URL | globalThis.Request,
590589
init?: RequestInit,
591590
) => {
591+
const mergedHeaders = { ...requestHeaders };
592+
// Only set Content-Type on requests with a body (e.g. POST).
593+
// GET requests (such as OAuth metadata discovery) must not
594+
// include Content-Type, as some servers reject it with 415.
595+
if (init?.body) {
596+
mergedHeaders["content-type"] = "application/json";
597+
}
592598
const response = await fetch(url, {
593599
...init,
594-
headers: requestHeaders,
600+
headers: mergedHeaders,
595601
});
596602

597603
// Capture protocol-related headers from response
@@ -611,11 +617,16 @@ export function useConnection({
611617
url: string | URL | globalThis.Request,
612618
init?: RequestInit,
613619
) => {
614-
requestHeaders["Accept"] =
615-
"text/event-stream, application/json";
616-
requestHeaders["Content-Type"] = "application/json";
620+
const mergedHeaders = { ...requestHeaders };
621+
mergedHeaders["Accept"] = "text/event-stream, application/json";
622+
// Only set Content-Type on requests with a body (e.g. POST).
623+
// GET requests (such as OAuth metadata discovery) must not
624+
// include Content-Type, as some servers reject it with 415.
625+
if (init?.body) {
626+
mergedHeaders["Content-Type"] = "application/json";
627+
}
617628
const response = await fetch(url, {
618-
headers: requestHeaders,
629+
headers: mergedHeaders,
619630
...init,
620631
});
621632

0 commit comments

Comments
 (0)