const response = await fetch(`/path/to/custom/endpoint`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(theData),
})I'm attempting to get the post body from the
reqobject in the endpoint handler:
const customEndpoint :Endpoint = {
path: '/path/to/custom/endpoint',
method: 'post',
root: true,
handler: async (req, res, next) => {
try {
console.log(req.body) // undefined :(
res.status(200).send({
message: 'Request successful',
})
}
catch (err) {
console.error(err)
res.status(500).send({
message: 'Error occurred during request',
error: err,
})
}
},
}I also attempted this with a
application/x-www-form-urlencodedcontent-type and nothing appears to be parsing the post body.
That's odd, you should get the body back, is the req undefined as well?
Also, is customEndpoint passed in the endpoints array of your Payload Config?
Also, how about req.params?
req.paramsreturns a
{}(sorry, multi-tasking lol)
Hey
@214419801039175682So I just added that endpoint to my payload config
const customEndpoint: Endpoint = {
path: "/foo",
method: "post",
handler: async (req, res, next) => {
try {
console.log(req.body);
res.status(200).send({
message: "Request successful",
});
} catch (err) {
console.error(err);
res.status(500).send({
message: "Error occurred during request",
error: err,
});
}
},
};I removed "root: true"
And then with postman I hit
http://localhost:3000/payload/api/foo with{"hello":"world"}`
And it looks like it logged the req.body correctly
niice! i did wonder if setting root to true was messing with things. I only did that in the first place because I wasn't aware the prefix was
/payload/apii'll give that a try and report back
Sounds good, let me know!
huh, yeah i don't know why i keep getting 404's when i attempt the removal of
root:truecopy/pasted your code in and it's still 404'ing
to answer your earlier question, yes, the endpoints is an array
and
reqitself is not undefined
just missing the body prop
hmm
Can we see your payload config?
sure sec
so all my config is in this plugin i'm writing which looks like this
import path from 'path'
import { Config, Plugin } from 'payload/config'
import AfterNavLinks from './afterNavLinks'
import endpoints from './endpoints'
import Terms from './collections/Terms'
import TermsManagerUi from './ui'
import TermsManagerImporterUi from './ui/importer'
const termsManager :Plugin = (incomingConfig :Config) :Config => {
const config :Config = {
...incomingConfig,
admin: {
...incomingConfig.admin,
components: {
...incomingConfig.admin.components,
routes: [
{
Component: TermsManagerUi,
path: '/tools/terms-manager',
},
{
Component: TermsManagerImporterUi,
path: '/tools/terms-manager-import',
},
],
afterNavLinks: [ AfterNavLinks ],
},
webpack: (config) => {
const newConfig = {
...(config || {}),
resolve: {
...(config.resolve || {}),
alias: {
...(config.resolve.alias || {}),
...[
'mssql',
'fs',
].reduce((c, i) => ({
...c,
[i]: path.resolve(__dirname, `mocks/emptyObject.js`)
}), {}),
},
},
}
return newConfig
},
},
endpoints: [ ...endpoints ],
collections: [
...incomingConfig.collections,
Terms,
],
}
return config
}
export default termsManagerthen in the original config, i just include the plugin
endpoints is an array of objects, right?
yup yup
what is the response code of the request?
200?
404 atm. when i set it to /foo without root
that is what the end request is yes
and that's a 404?
yes
ok what about
404?
the default prefix isnt /payload/api, i manually do that in my config
checking
i think yours should just be /api/
ohh ok, yes, /api/foo worked
NICE
and the post body came through
😄
Yayyyy
thank you so much!
Of course!
I think
root: trueis missing body parser
Hmm sorry what do you mean/
once you have
root: trueon the custom endpoint, req.body is empty. It's not empty when
root: falseStar
Discord
online
Get dedicated engineering support directly from the Payload team.