Hi guys , I've had this kind of issue before, and since I couldn't resolve it, I worked around it, but now I really need to understand why custom routes can't receive POST requests, as req.body is always undefined. By custom routes, I mean the routes defined in plugins with endpoints: (incoming.endpoints || []).concat([{...}])
Is this on version 2 or 3
v2
Hmm, I'm on V2 and I have custom endpoints that get req.body back
{
path: '/amp',
method: 'post',
handler: async (req, res, next) => {
const secret = process.env.PAYLOAD_AMP_SECRET;
// Check if the request had the AMP secret
if (secret !== req.headers.amp) {
return res.status(401).json({
message: 'Unauthorized.',
});
}
const { firstName, lastName, repid, email, password } = req.body;
....Now granted, this isn't defined from a plugin
But it should be the same
plugin/index.ts
const CLIENTSIDE = typeof session !== 'function'
export const plugin =
(options: pluginOptions) =>
(incoming: Config): Config => {
const config: Config = {
...incoming,
collections: [...(incoming.collections || []), ...plugin_collections],
}
return CLIENTSIDE ? pluginClient(config, options) : pluginServer(payload, session)(config, options)
}plugin/server/index.ts
export const pluginServer = (payload, session) => (
incoming: Config,
options: pluginOptions
): Config => {
return {
...incoming,
admin: {
...incoming.admin,
webpack: (webpackConfig) => {
const config = incoming.admin?.webpack?.(webpackConfig) || webpackConfig
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve?.alias,
...plugin.alias,
},
},
}
},
},
endpoints: (incoming.endpoints || []).concat([
{
path: '/route_1',
method: 'get',//instead of 'post'
root: true,
handler: route_1(payload, options)
}])
}
}I'm probably doing something wrong with the plugin because it's only when the route is defined in a plugin.
isnt it like
endpoints: [(...incoming.endpoints || []),
{
path: '/route_1',
method: 'get',//instead of 'post'
root: true,
handler: route_1(payload, options)
}]Either way, yeah, its gotta be how the custom endpoint is added
I think this is the same as what I did.
Do you get any response back from the endpoint?
the custom one
yes
the endpoint is fine but post req.body dont work
Currently, I'm doing it like this because POST doesn't work:
await fetch(${process.env.NEXT_PUBLIC_SERVER_URL_DEV}/route_1?arg1=${encodeURIComponent(JSON.stringify(arg1))}&arg2=${encodeURIComponent(JSON.stringify(arg2))})If you log req.body
What is the result
undefined
Can you get rid of root: true
on the custom endpoint, and try again
Okay, hold on a second, if we don't make progress like this, I'll create a minimal code on GitHub. Thanks a lot for your help once again!
No prob, I found someone with a similar issue, and removing root on the custom endpoint fixed the req.body issue
Without the root property, it works.
👑
NICEEE
When true, defines the endpoint on the root Express app, bypassing Payload handlers and the routes.api subpath. Note: this only applies to top-level endpoints of your Payload config, endpoints defined on collections or globals cannot be root.Sort of
ok" When true, defines the endpoint on the root Express app, bypassing Payload handlers and the routes.api subpath"
I dont get it 100% but the last part seems to say it wont work on collection / global types
Thank you so much for your help ❤️ have a great day ! 🌟
Star
Discord
online
Get dedicated engineering support directly from the Payload team.