Azure / Azure/static-web-apps

The API middleware obstructs image downloads, by altering Content-Type

Open
#1,237 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
346
Forks
67
PR merge metrics
No merged PRs in 30d

Description

- I have an SWA API endpoint running in node where I am trying to serve a jpg image to the browser.
- The image is fetched by index.js from an external endpoint that needs authentication.
- The image stream is delivered perfectly to index.js with the correct headers. I wait until the stream ends, but I can't figure out how to pipe the byte array through the SWA API middleware with the correct headers.
- As far as I can see the SWA API middleware is altering the defined Content-Type for no apparent reason?

My function.json looks like this:

{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"route": "image/{id}/{type}",
"methods": [
"get"
]
},
{
"type": "http",
"direction": "out",
"name": "res",
"dataType": "binary"
}
]
}

In index.js I set the headers like this:

context.res = {
body: buffer,
headers: {
"Content-Type": "image/jpeg"
}
}

But when I call the endpoint (from postman) I can see the Content-Type header value is overwritten:

**_image/jpeg; charset=utf-8_**

and the image file is unreadable for the client.

- Calling the external endpoint directly from postman works like a charm!
- What is the reason for this strange behaviour?
- How to mitigate it so we can serve an image from the SWA API?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.