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.

Docs for PATCH or POST operations

discord user avatar
alessiogr
2 years ago
2

I don't know what is required in the body of those requests. Are there any docs or examples for those?



Didnt find any docs but it seems to just need the fields + new values as stringified json in the body

  • default discord avatar
    generator1012 years ago

    Can you share an example here?

  • default discord avatar
    jakey___2 years ago

    Hey

    @360823574644129795

    ! I'm currently not quite understanding how to get a POST request to a custom endpoint working. It seems like you figured it out here potentially?



    On the client I tried something like this:


    const response = await fetch(`/path/to/custom/endpoint`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(theData),
    })


    and on the endpoint I have:



    const customEndpoint :Endpoint = {
        path: '/path/to/custom/endpoint',
        method: 'post',
        root: true,
        handler: async (req, res, next) => {
            try {
                // How do I get the post body???
                res.status(200).send({
                    message: 'Request successful',
                })
            }
            catch (err) {
                console.error(err)
                res.status(500).send({
                    message: 'Error occurred during request',
                    error: err,
                })
            }
        },
    }
    
    export default customEndpoint


    Still haven't figured out how to get the post body



    Looks like in my case I'm attempting to post

    application/json

    where in payload it only accepts

    application/x-www-form-urlencoded

    ?



    hm.. tried a test

    application/x-www-form-urlencoded

    post and

    req.body

    is still undefined



    feels like the typical bodyParser middleware isn't firing for custom endpoints. Perhaps this is actually different from the original post



    I made another post in case someone else runs into this :

    https://discordapp.com/channels/967097582721572934/1111352788253941862



    Turns out it had to do with setting

    root

    to

    true

    in the handler

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.