I am developing an application where I need to hit this endpoint https://api2.transloadit.com/assemblies to upload a file of any type. I wish to add authentication params from the server before hitting the final endpoint Please Note: It is multipart/formdata that I wish to edit in the pre handler hook This is my code:
fastify.register(require("fastify-http-proxy"), {
upstream: "https://api2.transloadit.com/assemblies",
undici: true,
prefix: "/api", // optional
http2: false,
preHandler(request, reply, next) {
// how to access the data in request body
console.log(request);
next();
},
});
But this is the output of request.body :
body: IncomingMessage {
_readableState: ReadableState {
objectMode: false,
highWaterMark: 16384,
buffer: BufferList { head: null, tail: null, length: 0 },
length: 0,
pipes: [],
flowing: null,
ended: false,
endEmitted: false,
reading: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
resumeScheduled: false,
errorEmitted: false,
emitClose: true,
autoDestroy: false,
destroyed: false,
errored: null,
closed: false,
closeEmitted: false,
defaultEncoding: 'utf8',
awaitDrainWriters: null,
multiAwaitDrain: false,
readingMore: true,
decoder: null,
encoding: null,
[Symbol(kPaused)]: null
},
_events: [Object: null prototype] { end: [Function: clearRequestTimeout] },
_eventsCount: 1,
_maxListeners: undefined,
socket: Socket {
connecting: false,
_hadError: false,
_parent: null,
_host: null,
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 8,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_sockname: null,
_pendingData: null,
_pendingEncoding: '',
server: [Server],
_server: [Server],
parser: [HTTPParser],
on: [Function: socketListenerWrap],
addListener: [Function: socketListenerWrap],
prependListener: [Function: socketListenerWrap],
_paused: false,
_httpMessage: [ServerResponse],
[Symbol(async_id_symbol)]: 53,
[Symbol(kHandle)]: [TCP],
[Symbol(kSetNoDelay)]: false,
[Symbol(lastWriteQueueSize)]: 0,
[Symbol(timeout)]: null,
[Symbol(kBuffer)]: null,
[Symbol(kBufferCb)]: null,
[Symbol(kBufferGen)]: null,
[Symbol(kCapture)]: false,
[Symbol(kBytesRead)]: 0,
[Symbol(kBytesWritten)]: 0,
[Symbol(RequestTimeout)]: undefined
}
If you want to pass along auth params to the third party, you can inject them by accessing the request.raw
part of the url if it's a url param based API key. In your case:
fastify.register(require("fastify-http-proxy"), {
upstream: "https://api2.transloadit.com/assemblies",
undici: true,
prefix: "/api", // optional
http2: false,
preHandler(request, _reply, next) {
request.raw.url = `${request.raw.url}&apikey=${MY_API_KEY}`;
console.log(request);
next();
},
});
If the auth params are header based, you'll actually want to define the rewriteRequestHeaders
function. This would look like:
fastify.register(require("fastify-http-proxy"), {
upstream: "https://api2.transloadit.com/assemblies",
undici: true,
prefix: "/api", // optional
http2: false,
replyOptions: {
rewriteRequestHeaders: (_, headers) => {
return {
...headers,
'x-api-key': MY_API_KEY
};
}
}
});