Hi, I'm trying to access the body of my requests and I can't make it work...
I'm getting Unexpected end of JSON input every time i fire a request to this :
path: '/test',
method: 'get',
handler: async (req) => {
const body = await req.json()
console.log(body)
return new Response('OK', { status: 200 })
},my IDE tells me
Cannot invoke an object which is possibly 'undefined'.
'req.json' is possibly 'undefined'.
looks like everyone else does it like this, why can't I ?
What is this? I don't recognize Next.js or Payload in this code.
this is my first try at making a custom endpoint, here is the full file that i reference in
endpoints: [
test,
],in a collection config
Let me try, I don't know they have this feature 😄
af
you are using GET
ah
get method don't have body
ofc...
i'm so bad 😩 i need a break
too much stress
haha, it's weekend, enjoying it instead of stress bro
my client has put me through this... at least i'm paid... but time for coffee
thank you !!
it doesn't work even with post...
try
req?.json && await req.json()or sth
typescript should indicate that
curl -X POST http://localhost:3000/api/media/test
{"errors":[{"message":"Something went wrong."}]with this the ide doesn't show typescript error anymore but the result is the same, the server logs Unexpected end of JSON input
show me full code
try to remove .next folder
stop dev then remove .next folder then start dev again
why your url is api/media/test?
i'm in a plugin create-payload-app template by the way, the endpoint is in
export default buildConfig({
admin: {
autoLogin: devUser,
importMap: {
baseDir: path.resolve(dirname),
},
},
collections: [
{
slug: 'posts',
fields: [],
},
{
slug: 'media',
fields: [],
upload: {
staticDir: path.resolve(dirname, 'media'),
},
endpoints: [test],
},
],do you try request to api/test?
Show me also fetch request, and please provide test.ts in discord brackets as I am on phone
it's api/media/test it returns error, /api/test returns 404 in server logs
import { Endpoint, PayloadRequest } from 'payload'
export const test: Endpoint = {
path: '/test',
method: 'post',
handler: async (req: PayloadRequest) => {
const body = req?.json && (await req.json())
console.log(body)
return new Response('OK', { status: 200 })
},
}could you show me how you make the request to /api/test?
Yeah show fetch request
oh i managed to do it ?
i used curl to do the request not a fetch()
i think i just had a bad syntax on -d it works now
copy and past the curl here, or screenshot it
Personally I recommend axios for frontend
will do !
don't worry it's okay now
🤡 you need go to bed dude
😄 good to hear it, i'm sleep now
it's 6pm here, but i do need to sleep for like a week like a polar bear
have a good night!!
we get this error internally in payload as well, in some cases the body isnt available OR the stream abruptly ends via cancellation or something from the browser
oh i'm not so crazy then !!
i don't know how i fixed it, i changed the syntax of my curl, i added allowImportingTsExtensions in ts.config, and i moved the endpoint file's relative path all at once and on next request it was working
Star
Discord
online
Get dedicated engineering support directly from the Payload team.