Giter VIP home page Giter VIP logo

azure-function-multipart's Introduction

azure-function-multipart

Build and Test codecov Codacy Badge npm downloads

Module to parse multipart/form-data on Azure Functions.

Note: Will also works on any Node's HTTP request object, asides of Azure Functions' HttpRequest object, as this package is based on busboy.

Install

npm i @anzp/azure-function-multipart

Examples

Parsing multipart/form-data on Azure Function. This will return the parsed fields and files back as the response.

For TypeScript:

import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import parseMultipartFormData from "@anzp/azure-function-multipart";

const httpTrigger: AzureFunction = async function (
  context: Context,
  req: HttpRequest
): Promise<void> {
  const { fields, files } = await parseMultipartFormData(req);
  context.log("HTTP trigger function processed a request.");
  const name = req.query.name || (req.body && req.body.name);
  const responseMessage = {
    fields,
    files,
  };

  context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage,
  };
};

export default httpTrigger;

For JavaScript:

const parseMultipartFormData = require("@anzp/azure-function-multipart")
  .default;

module.exports = async function (context, req) {
  const { fields, files } = await parseMultipartFormData(req);
  context.log("HTTP trigger function processed a request.");
  const name = req.query.name || (req.body && req.body.name);
  const responseMessage = {
    fields,
    files,
  };

  context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage,
  };
};

Example client request using CURL:

curl --request POST \
  --url http://localhost:7071/api/HttpTrigger1 \
  --header 'Content-Type: multipart/form-data; boundary=---011000010111000001101001' \
  --form update=false \
  --form collection=@/Users/anzhari/masterdata/managements.json

This is the example reponse received on the client:

{
  "fields": [
    {
      "fieldname": "update",
      "value": "false",
      "fieldnameTruncated": false,
      "valueTruncated": false,
      "encoding": "7bit",
      "mimetype": "text/plain"
    }
  ],
  "files": [
    {
      "fieldname": "file",
      "bufferFile": {
        "type": "Buffer",
        "data": [91, 10, ...10, 93]
      },
      "filename": "managements.json",
      "encoding": "7bit",
      "mimetype": "application/json"
    }
  ]
}

You can also pass busboy constructor config as an optional parameter:

import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import parseMultipartFormData from "@anzp/azure-function-multipart";

const httpTrigger: AzureFunction = async function (
  context: Context,
  req: HttpRequest
): Promise<void> {
  // Set the max number of non-file fields to 1 (Default: Infinity).
  const config = {
    limits: { fields: 1 },
  };
  const { fields, files } = await parseMultipartFormData(req, config);
  context.log("HTTP trigger function processed a request.");
  const name = req.query.name || (req.body && req.body.name);
  const responseMessage = {
    fields,
    files,
  };

  context.res = {
    // status: 200, /* Defaults to 200 */
    body: responseMessage,
  };
};

export default httpTrigger;

azure-function-multipart's People

Contributors

anzharip avatar dependabot-preview[bot] avatar dependabot[bot] avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

azure-function-multipart's Issues

Support of Azure Functions Node.js v4 programming model

We've been using this package successfully for our Azure Functions using v3 programming model (with function.json files).
When we started our migration to v4 programming model we discovered that the parsing of multipart form data fails due to Exception: Missing Content-Type, even if headers from the request are identical as before.

Whats seems have changed is that @azure/functions package use a different definition of HttpRequest in version 4.

At the moment we are using the following workaround, but it would be preferable with support in @anzp/azure-function-multipart package:

const headers = {}
for (const [key, value] of request.headers.entries()) {
  headers[key] = value
}

const { files, fields } = await parseMultipartFormData(
  { ...request, body: Buffer.from(await request.arrayBuffer()) },
  { headers }
)

TypeError: parseMultipartFormData is not a function

Hi, I get this error.

Code:

const parseMultipartFormData = require('@anzp/azure-function-multipart')

module.exports = async function (context, req) {
const { fields, files } = await parseMultipartFormData(req)
}
...

Function Runtime Version: 4.0.1.16815

What could be wrong?

Not working with new runtime

Yesterday, I've updated to the new Azure Functions Core Tools

Azure Functions Core Tools
Core Tools Version: 4.0.4544 Commit hash: N/A (64-bit)
Function Runtime Version: 4.3.2.18186

It seems that busboy has some problems, the events don't get fired any more and the functions runs endlessly.
I'm currently trying to find the root cause.
It doesn't work locally neither on Azure.

parseMultipartFormData() type error

Getting the following error now when I try to pass only the request object from my Azure Function to parseMultipartFormData():

Argument of type 'HttpRequest' is not assignable to parameter of type 'HttpRequest & { headers: { "content-type": string; }; }'.
Type 'HttpRequest' is not assignable to type '{ headers: { "content-type": string; }; }'.
Types of property 'headers' are incompatible.
Property '"content-type"' is missing in type 'HttpRequestHeaders' but required in type '{ "content-type": string; }'.

const { fields, files } = await parseMultipartFormData(req);

Update "@azure/functions": "^3.2.0" to "^4.0.0"

error TS2345: Argument of type 'import("C:/work/node_modules/@azure/functions/types/http").HttpRequest' is not assignable to parameter of type 'import("C:/work/node_modules/@anzp/azure-function-multipart/node_modules/@azure/functions/types/http").HttpRequest'.
  Type 'HttpRequest' is missing the following properties from type 'HttpRequest': get, parseFormBody

29     const { fields, files } = await parseMultipartFormData(request);
                                                              ~~~~~~~

#373 is similar to this Issue, but I believe the cause is different.

How can I use this with CommonJS?

Hi, I browsed your library and I feel that it might help me solve a direct issue I'm having with multipart form data. However, I can't import using CommonJS and I don't have the liberty of upgrading the runtime to support ES Modules. Is there a way I could bypass the problem and if not, do you think you could support this? Thanks.

Bug in readme

Hi @anzharip

in the readme we have

{
      "fieldname": "update",
      "value": "false",
      "fieldnameTruncated": false,
      "valueTruncated": false,
      "encoding": "7bit",
      "mimetype": "text/plain"
    }

this is wrong - should be:

{
      "name": "update",
      "value": "false",
      "nameTruncated": false,
      "valueTruncated": false,
      "encoding": "7bit",
      "mimetype": "text/plain"
    }

Perhaps you forgot to update the readme. ๐Ÿ˜

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.