Azure / Azure/Azure-Functions

Node async function binary response

Open
#1,344 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
1.1k
Forks
215
Avg merge
4h 2m
Merged PRs (30d)
1

Description

I'm returning a binary file with `Content-Type: image/png` from my node function.

Using async functions/Promises works:

```js
module.exports = function(context, req) {
return new Promise((resolve, reject) => {
fs.readFile(__dirname + "/image.png", function(err, data) {
if (!err) {
resolve({
status: 200,
body: data,
isRaw: true,
headers: {
"Content-Type": "image/png"
}
});
} else {
// reject(...)
}
});
});
};
```

...but the response gets `Content-Type: image/png; charset=utf-8` and looks like:

```
{"type":"Buffer","data":[137,80,78,71,13,10,26,10,0,0,0,13,73,72...
```

But the equivalent "traditional" calling convention works fine:

```js
module.exports = function(context, req) {
fs.readFile(__dirname + "/image.png", function(err, data) {
if (!err) {
context.res = {
status: 200,
body: data,
isRaw: true,
headers: {
"Content-Type": "image/png"
}
};
} else {
context.res = {
// error
};
}
context.done();
});
};
```

...and I can download an uncorrupted PNG file just fine.

Any idea why this happens? Specifically, it looks like `Buffer` is a valid type for a raw response in one calling convention, but but not the other.

I'm using platform version `~2` and `WEBSITE_NODE_DEFAULT_VERSION = "10.14.1"`, if it matters.

For the former example, I have:

```json
{
"type": "http",
"direction": "out",
"name": "$return"
}
```

in my `function.json`, and for the latter:

```json
{
"type": "http",
"direction": "out",
"name": "res"
}
```

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.