我正在使用fastify代理对spring引导后端的调用。使用fastify-http-代理作为代理和application/x form-urlencoded内容类型。为了支持它,我使用的是法西斯形式的身体。如果我直接调用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,
});
}
}
发布于 2022-04-18 06:35:10
问题是proxyPayload参数。它应该移除。链接讨论git。
https://stackoverflow.com/questions/71901403
复制相似问题