Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

req.body is undefined when using custom endpoint (method:"post")

default discord avatar
vpjmlast year
20

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([{...}])

  • default discord avatar
    notchrlast year

    Is this on version 2 or 3

  • default discord avatar
    vpjmlast year

    v2

  • default discord avatar
    notchrlast year

    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

  • default discord avatar
    vpjmlast year

    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.

  • default discord avatar
    notchrlast year

    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

  • default discord avatar
    vpjmlast year

    I think this is the same as what I did.

  • default discord avatar
    notchrlast year

    Do you get any response back from the endpoint?



    the custom one

  • default discord avatar
    vpjmlast year

    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))})
  • default discord avatar
    notchrlast year

    If you log req.body



    What is the result

  • default discord avatar
    vpjmlast year

    undefined

  • default discord avatar
    notchrlast year

    Can you get rid of root: true



    on the custom endpoint, and try again

  • default discord avatar
    vpjmlast year

    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!

  • default discord avatar
    notchrlast year

    No prob, I found someone with a similar issue, and removing root on the custom endpoint fixed the req.body issue

  • default discord avatar
    vpjmlast year

    Without the root property, it works.



    👑

  • default discord avatar
    notchrlast year

    NICEEE



    dances
  • default discord avatar
    notchrlast year
    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

  • default discord avatar
    vpjmlast year

    ok" When true, defines the endpoint on the root Express app, bypassing Payload handlers and the routes.api subpath"

  • default discord avatar
    notchrlast year

    I dont get it 100% but the last part seems to say it wont work on collection / global types

  • default discord avatar
    vpjmlast year

    Thank you so much for your help ❤️ have a great day ! 🌟

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.