Fix multipart parsing in NIP-95 upload proxy by removing manual Content-Type header and improving error handling

This commit is contained in:
Nicolas Cantu 2025-12-29 00:23:00 +01:00
parent 970f8761ac
commit 7bab834f89
2 changed files with 10 additions and 8 deletions

View File

@ -77,11 +77,7 @@ async function tryUploadEndpoint(endpoint: string, formData: FormData, useProxy:
const response = await fetch(targetUrl, { const response = await fetch(targetUrl, {
method: 'POST', method: 'POST',
body: formData, body: formData,
...(useProxy && { // Don't set Content-Type manually - browser will set it with boundary automatically
headers: {
'Content-Type': 'multipart/form-data',
},
}),
}) })
if (!response.ok) { if (!response.ok) {

View File

@ -24,15 +24,21 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
try { try {
// Parse multipart form data // Parse multipart form data
// formidable needs the raw Node.js IncomingMessage, which NextApiRequest extends
const form = new IncomingForm({ const form = new IncomingForm({
maxFileSize: MAX_FILE_SIZE, maxFileSize: MAX_FILE_SIZE,
keepExtensions: true, keepExtensions: true,
}) })
const parseResult = await new Promise<ParseResult>((resolve, reject) => { const parseResult = await new Promise<ParseResult>((resolve, reject) => {
form.parse(req, (err, fields, files) => { // Cast req to any to work with formidable - NextApiRequest extends IncomingMessage
if (err) reject(err) form.parse(req as any, (err, fields, files) => {
else resolve({ fields: fields as Record<string, string[]>, files: files as Record<string, FormidableFile[]> }) if (err) {
console.error('Formidable parse error:', err)
reject(err)
} else {
resolve({ fields: fields as Record<string, string[]>, files: files as Record<string, FormidableFile[]> })
}
}) })
}) })