file upload fix?

This commit is contained in:
2026-03-25 14:30:45 +01:00
parent fe2489b4ed
commit da0c3b5b67

View File

@@ -14,8 +14,10 @@ export const ALL: APIRoute = async ({ request, params }) => {
url.search = requestUrl.search;
const headers = new Headers();
// Filter headers to avoid conflicts. Content-Type is handled automatically for POST/PUT if we pass arrayBuffer.
const forbiddenHeaders = ['host', 'connection', 'origin', 'referer', 'content-length', 'transfer-encoding'];
// We must NOT forward 'host' (causes backend to reject if it doesn't match)
// We must NOT forward 'connection'
// We should let fetch recalculate 'content-length'
const forbiddenHeaders = ['host', 'connection', 'content-length', 'transfer-encoding', 'origin', 'referer'];
request.headers.forEach((value, key) => {
if (!forbiddenHeaders.includes(key.toLowerCase())) {
@@ -23,23 +25,28 @@ export const ALL: APIRoute = async ({ request, params }) => {
}
});
try {
console.log(`[Proxy] ${request.method} ${requestUrl.pathname} -> ${url.toString()}`);
let body: any = undefined;
if (request.method !== 'GET' && request.method !== 'HEAD') {
body = await request.arrayBuffer();
}
console.log(`\n[Proxy Req] ${request.method} -> ${url.toString()}`);
console.log(`[Proxy Req] Auth Header Present:`, headers.has('authorization'));
console.log(`[Proxy Req] Content-Type:`, headers.get('content-type'));
const response = await fetch(url.toString(), {
try {
const fetchOptions: RequestInit = {
method: request.method,
headers: headers,
body: body,
// @ts-ignore
duplex: 'half'
});
};
console.log(`[Proxy] Backend returned ${response.status} for ${url.toString()}`);
// Only attach body for methods that allow it
if (request.method !== 'GET' && request.method !== 'HEAD') {
// Pass the raw stream directly. This is crucial for multipart/form-data.
fetchOptions.body = request.body;
// Required by Node.js fetch when body is a ReadableStream
// @ts-ignore
fetchOptions.duplex = 'half';
}
const response = await fetch(url.toString(), fetchOptions);
console.log(`[Proxy Res] Backend returned ${response.status}`);
const responseHeaders = new Headers();
response.headers.forEach((value, key) => {