fastify-formbody 和 fastify-http-proxy 正在将 application/x-www-form-urlencoded 请求更改为字符串

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

我正在使用 fastify 来代理对 spring 引导后端的调用。使用 fastify-http-proxy 作为代理和 application/x-www-form-urlencoded 内容类型。为了支持它,我使用 fastify-formbody。 如果我直接调用 spring 引导后端,我会看到类似

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

但是当我使用代理调用时,我的请求就像(为值添加引号)

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

我的代理看起来像这样:

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,
    });
  }
}

问题出在 proxyPayload 参数上。它应该被删除。 Link 讨论 git。