fastify-formbody and fastify-http-proxy are changing application/x-www-form-urlencoded request to string
SamvelS opened this issue · 4 comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
3.28.0
Plugin version
6.2.2
Node.js version
14
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
12.2
Description
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,
});
}
}
Steps to Reproduce
Create proxy project as on code in description and proxy calls to spring boot app
Expected Behavior
No response
Why are you using formbody? I think it should work without.
I was getting Unsupported Media Type: application/x-www-form-urlencoded error and based on stackoverflow thread I need to use formbody.
I think you will need to remove fastify-formbody and remove proxyPayloads: false.
Thanks for help. That solved my issue. Closing this item.