Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rahul2123 opened this issue Aug 9, 2020 · 12 comments · Fixed by fastify/fastify-request-context#12
Closed

Comments

@rahul2123
Copy link

rahul2123 commented Aug 9, 2020

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

@rahul2123
Copy link
Author

Can anyone please help me with this, I am stuck because of this.

@Eomm
Copy link
Member

Eomm commented Aug 10, 2020

I'm writing on SO 💯

Till now: I can't replicate

@rahul2123
Copy link
Author

https://github.com/rahul2123/fastify-server
Here's the repository with the typescript configurations

@rahul2123
Copy link
Author

curl -X POST http://localhost:8085/test
using this curl command I can access the request context in the handlers,
but when I am sending post data
i.e curl -X POST http://localhost:8085/test -H "Content-Type: application/json" -d '{"username":"abc","password":"abc"}'
I am not able to access context data

@RafaelGSS
Copy link
Member

@rahul2123 could provide information about the versions? (fastify, fastify-request-context, nodejs)

@Eomm
Copy link
Member

Eomm commented Aug 10, 2020

For what I saw, it is related to the asynchronous-local-storage module usage, I didn't dig deeper tho

cc @kibertoad

@kibertoad
Copy link
Member

Thanks! I'll investigate.

@rahul2123
Copy link
Author

@RafaelGSS here the information that is needed

  • node version*: v9.5.0, v10.13.0, v12.18.3
  • fastify version*: 3.1.1
  • fastify-request-context* : 2.0.0

@kibertoad
Copy link
Member

@rahul2123 Please take a look at fastify/fastify-request-context#12. I believe it reproduces the way you use it more or less faithfully, and still works. Note that minimum supported Node version is 10.16.0, so maybe you could try with a slightly newer Node version and see if that helps? That wouldn't explain why 12.18.3 is not working for you, though, as it is exactly the same version that works in CI.
If you could submit a PR to https://github.com/fastify/fastify-request-context/ with a test that would be failing, that would help tremendously; you can use one of the existing tests as a base.

@kibertoad
Copy link
Member

@rahul2123 OK, I managed to reproduce that after adding end-to-end test without light-my-request. Will investigate further.

@kibertoad
Copy link
Member

@rahul2123 Basically the problem was that async storage was initialized too late in the lifecycle, and it was impossible to do so earlier. Added configuration option that should resolve your problem. Please review fastify/fastify-request-context#12

@kibertoad
Copy link
Member

@rahul2123 Please try 2.0.1, it should work as expected with no changes on your end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants