-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
body must be type object #1046
Comments
I have tried just about every obvious thing you could think of.
I'm really struggling for what to try next. The type of the thing I'm sending as the body is definitely an object... |
Could you add a Minimal, Reproducible Example? Without it, we are unable to help you. |
Minimally Reproducable ExampleFresh installation of fastify. This is the const fastify = require('fastify')()
const schema = {
handler: (req,res) => res.send({msg: 'zomg'}),
schema: {
body: {
type: 'object',
required: ['name'],
properties: {
value: {type:'object'},
name: {type:'string'}
}}
}
}
fastify.register((fast, opts, done) => {
fast.post('/rofl', schema)
done()
})
const start = async () => {
try { await fastify.listen({port: 1337, host:'0.0.0.0'}) }
catch (e) { fastify.log.error(e);process.exit(1) }
}
start() Now open a chrome browser console and paste in the following const submit = async (event) => {
const url = 'http://localhost:1337/rofl'
const json = await fetch(url, {
method:'POST',
mode:'no-cors',
body: JSON.stringify({
name: 'bob',
value: {}
})
}).then(data => data.text())
.then(text => console.log(text))
.catch(err => console.log(err))
}
submit() producing the following server log {
"level": 30,
"time": 1722607306150,
"pid": 252188,
"hostname": "ITDEVNB-01",
"reqId": "req-1",
"res": {
"statusCode": 400
},
"err": {
"type": "Error",
"message": "body must be object",
"stack": "Error: body must be object\n at defaultSchemaErrorFormatter (/home/n/apps/repro/node_modules/fastify/lib/context.js:114:10)\n at wrapValidationError (/home/n/apps/repro/node_modules/fastify/lib/validation.js:228:17)\n at validate (/home/n/apps/repro/node_modules/fastify/lib/validation.js:146:16)\n at preValidationCallback (/home/n/apps/repro/node_modules/fastify/lib/handleRequest.js:92:25)\n at handler (/home/n/apps/repro/node_modules/fastify/lib/handleRequest.js:76:7)\n at /home/n/apps/repro/node_modules/fastify/lib/contentTypeParser.js:199:9\n at AsyncResource.runInAsyncScope (node:async_hooks:206:9)\n at done (/home/n/apps/repro/node_modules/fastify/lib/contentTypeParser.js:192:14)\n at Parser.defaultPlainTextParser [as fn] (/home/n/apps/repro/node_modules/fastify/lib/contentTypeParser.js:309:3)\n at IncomingMessage.onEnd (/home/n/apps/repro/node_modules/fastify/lib/contentTypeParser.js:283:27)",
"statusCode": 400,
"code": "FST_ERR_VALIDATION",
"validation": [
{
"instancePath": "",
"schemaPath": "#/type",
"keyword": "type",
"params": {
"type": "object"
},
"message": "must be object"
}
],
"validationContext": "body"
},
"msg": "body must be object"
} |
It is a cors issue due to the
In detail:
So, no If you log it:
You will see:
You need to fix your FE/BE cors setup. |
💬 body must be type object
I have a very simple POST end point, which is invalidating the most bare-bones request so that I don't even know how to troubleshoot further. I have a barebones fastify instance with only 1 other end point, running on the same local machine as a browser application trying to make a POST request via the fetch API bundled with my browser, which is up-to-date Chrome.
Fastify Schema
Client JS
Fastify Output
Your Environment
The text was updated successfully, but these errors were encountered: