Fastify (v5.3.2) returns 413 Payload Too Large for 2MB JSON despite 50MB bodyLimit

Discussão em 'Angular' iniciado por Muthukani K, Maio 8, 2025.

  1. Muthukani K

    Muthukani K Guest

    I'm using Fastify v5.3.2 in an Angular library (Angular v15), and running the Fastify server with the following script in package.json:

    "scripts": {
    "serve": "node --experimental-worker --no-warnings ./transpile/src/lib/server.js",
    "serve-prod": "node --experimental-worker --no-warnings ./dist/index.js",
    "transpile": "npx tsc",
    "lint": "eslint './transpile/**/*.ts' --fix",
    "pre-test": "npm run transpile",
    "test": "npm run pre-test && npx mocha \"transpile/test\"",
    "build": "npx tsc && browserify ./transpile/src/lib/server.js -o ./dist/index.js --node && node ./build/post-process.js"
    }


    My Fastify server.ts is configured like this:

    import "zone.js";
    import ComponentGenerator from "./component-generator";
    import Fastify from 'fastify';
    import type { Template } from '../lib/models/template';

    const server = Fastify({
    logger: true,
    bodyLimit: 52428800, // 50MB
    });

    const port = process.env.DYNAMIC_PORT as string;

    server.post("/template/renderTemplate", async (request: any) => {
    const templateId = request.body.templateId as string;
    const data = request.body.data;
    const template = request.body.template as Template;
    const renderer = new ComponentGenerator();
    return renderer.renderTemplate(templateId, data, template);
    });

    const start = async () => {
    try {
    await server.listen({ port: parseInt(port), host: "0.0.0.0" });
    } catch (err) {
    server.log.error(err);
    process.exit(1);
    }
    };

    start();


    From my C# backend (hosted in Azure Service Fabric), I’m calling this endpoint like so:

    var request = new HttpRequestMessage(HttpMethod.Post, angularservice + "template/renderTemplate");
    var json = JsonConvert.SerializeObject(template);
    _logger.LogInformation($"Payload size: {Encoding.UTF8.GetByteCount(json)} bytes");
    request.Content = new StringContent(json, Encoding.UTF8, "application/json");
    _logger.LogInformation($"Calling fastify.");
    using var response = await _http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
    response.EnsureSuccessStatusCode();


    Even though the payload is only around 2MB, I consistently get a 413 Payload Too Large error. The Fastify bodyLimit is set to 50MB, so I’m not sure why this is happening.

    Is there something I’m missing in the Fastify configuration, or could this be related to how the request is being proxied or handled in Azure Service Fabric?

    Continue reading...

Compartilhe esta Página