Skip to content

Azure Functions (node) doesn't process binary data properly #294

@christian-vorhemus

Description

@christian-vorhemus

UPDATE: Per final design, we added a property request.bufferBody that you can use to access the binary data


My goal is to write a function which processes the binary HTTP body (for example, an image). I've created the function using the func CLI (version 2.7.2254) doing the following:

func init --worker-runtime "node" --language "typescript"
func new --name "SampleFunction" --template "HTTP trigger"
npm install
npm install @types/node --save

My function.json looks like this:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "dataType": "binary",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/SampleFunction/index.js"
}

My expectation would be that now req.body is a Buffer but when I check the type
typeof req.body;

it seems to be a string. Therefore, my attempt to save this file to disk like you see below in my index.ts also fails:

import { AzureFunction, Context, HttpRequest } from "@azure/functions"
const fs = require('fs');

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    var file = req.body;
    fs.writeFileSync("file.png", file, 'binary');
};

export default httpTrigger;

Test:

curl -X POST --data-binary "@C:\path\to\image.png" http://localhost:7
071/api/SampleHttpFunction

What's the correct way of processing binary data in a node based Azure Function?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1v4 model 🚀Related to the new V4 programming model

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions