Skip to content

body must be type object #1046

Closed as not planned
Closed as not planned
@nathan-rogers

Description

@nathan-rogers

💬 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

{
  handler: (req, res) => res.send({status: 200, msg:'success'}),
  schema: {
    description: 'submit a filled form',
    body: { 
      type: 'object', 
      required: ['name'],
      properties: { 
        value: {type:'object'}, 
        name:  {type:'string'}
    }}
  }
}

Client JS

    const result = await fetch(url, {
      method:'POST',
      mode:'no-cors', // because I'm running locally atm
      body: JSON.stringify({
        name: 'bob',
        value: {}
      })
    }).then(data => data.text())
      .then(text => console.log(text))
      .catch(err => console.log(err))

Fastify Output

{
  "level": 30,
  "time": 1722528339845,
  "pid": 246204,
  "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/api/node_modules/fastify/lib/context.js:114:10)\n    at wrapValidationError (/home/n/apps/api/node_modules/fastify/lib/validation.js:228:17)\n    at validate (/home/n/apps/api/node_modules/fastify/lib/validation.js:146:16)\n    at preValidationCallback (/home/n/apps/api/node_modules/fastify/lib/handleRequest.js:91:25)\n    at handler (/home/n/appsapi/node_modules/fastify/lib/handleRequest.js:75:7)\n    at /home/n/apps/api/node_modules/fastify/lib/contentTypeParser.js:199:9\n    at AsyncResource.runInAsyncScope (node:async_hooks:206:9)\n    at done (/home/n/apps/api/node_modules/fastify/lib/contentTypeParser.js:192:14)\n    at Parser.defaultPlainTextParser [as fn] (/home/n/apps/api/node_modules/fastify/lib/contentTypeParser.js:309:3)\n    at IncomingMessage.onEnd (/home/n/apps/api/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"
}

Your Environment

  • node version: v20.11.1
  • fastify version: ^4.26.2
  • os: Ubuntu 22.04.4 LTS
  • browser: Chrome (up to date)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions