spring-bootservletsfastifynode-http-proxyx-www-form-urlencoded

fastify-formbody and fastify-http-proxy are changing application/x-www-form-urlencoded request to string


I am using fastify to proxy calls to spring boot backend. Using fastify-http-proxy as proxy and application/x-www-form-urlencoded content type. To support it I am using fastify-formbody. If I do direct call to spring boot back end, I see request like

parsedFormData=FormData{values={foo=[io.undertow.server.handlers.form.FormData$FormValueImpl@7848eda9], bar=[io.undertow.server.handlers.form.FormData$FormValueImpl@22f0cd6c]}}

But when I do call with proxy, my request is like (quotes are added for values)

parsedFormData=FormData{values={"foo=[io.undertow.server.handlers.form.FormData$FormValueImpl@7848eda9], bar=[io.undertow.server.handlers.form.FormData$FormValueImpl@22f0cd6c]}}

My proxy looks like this:

import { FastifyHttpsOptions, FastifyReply, FastifyRequest } from 'fastify';
import fastifyHttpProxy from 'fastify-http-proxy';
import * as qs from 'qs';

export class ProxyRoute {
  public registerProxy(app, prefix: string, rewritePrefix: string) {
    if (app.conf == undefined) {
      app.decorate('conf', {});
    }
    app.register(fastifyHttpProxy, {
      contentTypesToEncode: ['application/x-www-form-urlencoded'],
      upstream: '',
      prefix: prefix,
      rewritePrefix: rewritePrefix,
      replyOptions: {
        getUpstream: () => app.conf.hostUrl,
        rewriteRequestHeaders: (_req: FastifyRequest, headers: FastifyHttpsOptions<any>) => ({
          ...headers,
          sessionID: app.conf.sessionID,
        }),
      },
      preHandler: async (req: FastifyRequest, res: FastifyReply) => {
         
          if (contentType && contentType.includes('application/x-www-form-urlencoded')) {
            req.body = qs.stringify(req.body);
          }
      },
      proxyPayloads: false,
    });
  }
}

Solution

  • The issue was with proxyPayload parameter. It should be removed. Link to discussion on git.