file upload fix?
This commit is contained in:
@@ -14,8 +14,10 @@ export const ALL: APIRoute = async ({ request, params }) => {
|
|||||||
url.search = requestUrl.search;
|
url.search = requestUrl.search;
|
||||||
|
|
||||||
const headers = new Headers();
|
const headers = new Headers();
|
||||||
// Filter headers to avoid conflicts. Content-Type is handled automatically for POST/PUT if we pass arrayBuffer.
|
// We must NOT forward 'host' (causes backend to reject if it doesn't match)
|
||||||
const forbiddenHeaders = ['host', 'connection', 'origin', 'referer', 'content-length', 'transfer-encoding'];
|
// 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) => {
|
request.headers.forEach((value, key) => {
|
||||||
if (!forbiddenHeaders.includes(key.toLowerCase())) {
|
if (!forbiddenHeaders.includes(key.toLowerCase())) {
|
||||||
@@ -23,23 +25,28 @@ export const ALL: APIRoute = async ({ request, params }) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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'));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log(`[Proxy] ${request.method} ${requestUrl.pathname} -> ${url.toString()}`);
|
const fetchOptions: RequestInit = {
|
||||||
|
|
||||||
let body: any = undefined;
|
|
||||||
if (request.method !== 'GET' && request.method !== 'HEAD') {
|
|
||||||
body = await request.arrayBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await fetch(url.toString(), {
|
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers: headers,
|
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();
|
const responseHeaders = new Headers();
|
||||||
response.headers.forEach((value, key) => {
|
response.headers.forEach((value, key) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user