Fix: Proxy uses arrayBuffer for audio/image responses (not text)
Binary data (MP3 audio) was corrupted by resp.text(). Now detects content-type and uses arrayBuffer() for audio/* and image/* responses. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -20,10 +20,21 @@ async function proxyRequest(
|
|||||||
fetchOptions.body = await request.text()
|
fetchOptions.body = await request.text()
|
||||||
}
|
}
|
||||||
const resp = await fetch(url, fetchOptions)
|
const resp = await fetch(url, fetchOptions)
|
||||||
|
const contentType = resp.headers.get('Content-Type') || 'application/json'
|
||||||
|
|
||||||
|
// Binary responses (audio, images) must use arrayBuffer, not text
|
||||||
|
if (contentType.startsWith('audio') || contentType.startsWith('image')) {
|
||||||
|
const buffer = await resp.arrayBuffer()
|
||||||
|
return new NextResponse(buffer, {
|
||||||
|
status: resp.status,
|
||||||
|
headers: { 'Content-Type': contentType },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const data = await resp.text()
|
const data = await resp.text()
|
||||||
return new NextResponse(data, {
|
return new NextResponse(data, {
|
||||||
status: resp.status,
|
status: resp.status,
|
||||||
headers: { 'Content-Type': resp.headers.get('Content-Type') || 'application/json' },
|
headers: { 'Content-Type': contentType },
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return NextResponse.json({ error: String(e) }, { status: 502 })
|
return NextResponse.json({ error: String(e) }, { status: 502 })
|
||||||
|
|||||||
Reference in New Issue
Block a user