Skip to content

fastify-request-context is not working for POST API calls #236

@rahul2123

Description

@rahul2123
import fastify from 'fastify';
const { fastifyRequestContextPlugin } = require('fastify-request-context');
const server = fastify({
    logger: true
});
const preValidationFn = (req, reply, done): any => {
    req.requestContext.set('hello', 'hello world');
    console.log(req.requestContext.get('hello'));
    done();
};
const handlerFn = (req, reply): any => {
    console.log(req.requestContext.get('hello'));
    reply.send('hello world');
};
server.route({
    url: '/test',
    method: ['GET', 'POST'],
    preValidation: preValidationFn,
    handler: handlerFn
});

server.register(fastifyRequestContextPlugin);
console.log('Starting fastify Typescript server ');
server.listen(8085, '0.0.0.0', (err, address): void => {
    if (err) {
        console.warn(err);
        process.exit(1);
    }
    console.info(`Server listening at ${address}`);
});


process.on('uncaughtException', error => {
    console.error(error);
});
process.on('unhandledRejection', error => {
    console.error(error);
});

This is my fastify server written in typescript while using the fastify-request-context plugin I am able to access the context of fastify request when I am requesting the resource using the GET method, But I am not able to access the context of fastify request if I am requesting the resource using POST method

This is the stackoverflow link

https://stackoverflow.com/questions/63325150/fastify-request-context-is-not-working-for-post-api-calls

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions