You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Node.js及Azure Function中解析带boundary的multipart/form-data获取RawRequest对象

How to Extract the rawRequest Object in Your Node.js Azure Function

Alright, let's break down how to pull that rawRequest object from your multipart/form-data POST request in a Node.js Azure Function. Since Azure Functions doesn't automatically parse multipart data out of the box, we'll use a lightweight library called busboy to handle the parsing smoothly.

Step 1: Install the Required Library

First, add busboy to your project dependencies. Run this command in your function's root directory:

npm install busboy

Step 2: Update Your HttpTrigger Code

Replace your existing function code with the snippet below. It handles multipart parsing, locates the rawRequest field, and converts its value into a usable JavaScript object:

const busboy = require('busboy');

module.exports = async function (context, req) {
    return new Promise((resolve, reject) => {
        // Initialize busboy with request headers to respect the specified boundary
        const bb = busboy({ headers: req.headers });

        // Listen for form fields being parsed
        bb.on('field', (name, val) => {
            if (name === 'rawRequest') {
                try {
                    // Convert the JSON string into a workable object
                    const rawRequestObj = JSON.parse(val);
                    context.log('Successfully parsed rawRequest:', rawRequestObj);

                    // Use the parsed object for your business logic here
                    context.res = {
                        status: 200,
                        body: {
                            message: 'Raw request parsed successfully',
                            data: rawRequestObj
                        }
                    };
                    resolve();
                } catch (err) {
                    context.log.error('Failed to parse rawRequest JSON:', err);
                    context.res = {
                        status: 400,
                        body: { error: 'Invalid JSON format in rawRequest field' }
                    };
                    resolve();
                }
            }
        });

        // Handle the end of request parsing (in case rawRequest is missing)
        bb.on('finish', () => {
            if (!context.res) {
                context.res = {
                    status: 400,
                    body: { error: 'rawRequest field not found in the request' }
                };
                resolve();
            }
        });

        // Catch any parsing errors
        bb.on('error', (err) => {
            context.log.error('Multipart parsing error:', err);
            context.res = {
                status: 500,
                body: { error: 'Failed to process multipart request' }
            };
            reject(err);
        });

        // Pipe the request body into busboy for processing
        req.pipe(bb);
    });
};

Quick Tips to Avoid Issues

  • Fix JSON Syntax: Your example payload has a syntax error ("q1_nombres":"first":"textbox_sample0" should be "q1_nombres": {"first":"textbox_sample0"}). Ensure the rawRequest value is valid JSON, otherwise the JSON.parse step will fail.
  • Check Function Config: Make sure your function.json allows POST requests and sets an appropriate authLevel (e.g., anonymous for testing, function for production).
  • Node.js Version: This works best with Node.js 16 or later, which is compatible with the latest busboy version and Azure Functions v4.

内容的提问来源于stack exchange,提问作者Vincenz Lachner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:30:21