-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationgood first issueGood for newcomersGood for newcomers
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
v4.26.2
Plugin version
No response
Node.js version
20
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
20.04
Description
addContentTypeParser doesn't work as expected when previous statement is register with await.
Steps to Reproduce
ex:
'use strict'
const main = async () => {
const server = require('fastify')({
logger: true,
});
const schema = {
schema: {
response: {
200: {
type: 'object',
properties: {
hello: {
type: 'string',
},
},
},
},
},
};
await server.register((fastify, options, done) => {
fastify.get('/api/upload', async (req, res) => {
return { hello: 'world0' };
})
fastify.post('/api/upload', async (req, res) => {
return { hello: 'world1' };
})
done();
});
server.get('/', schema, function (req, res) {
res.send({ hello: 'world2' });
});
// This parser will not work after "server.register" in "await"
server.addContentTypeParser(
'text/csv',
{ parseAs: 'string' },
(req, body, done) => {
// Custom parsing logic for text/csv content type for api plugin
const csvData = body.split('\n').map((row) => row.split(','));
done(null, csvData);
},
);
server.listen({ port: 3000 }, (err, address) => {
if (err) {
throw err;
}
});
};
main();
curl -H "Content-Type: text/csv" -X POST --data "a,b,c" http://localhost:3000/api/upload
// you will get below
{"statusCode":415,"code":"FST_ERR_CTP_INVALID_MEDIA_TYPE","error":"Unsupported Media Type","message":"Unsupported Media Type: text/csv"}
Expected Behavior
the CSV parser should work as expected.
curl -H "Content-Type: text/csv" -X POST --data "a,b,c" http://localhost:3000/api/upload
{"hello":"world1"}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationgood first issueGood for newcomersGood for newcomers