400 Bad Request
The server couldn’t understand your request because the syntax is bad, the data is malformed, or it’s missing required fields.
When and Why to Use It
Section titled “When and Why to Use It”Use this when the client sends you garbage. E.g., they sent invalid JSON, they missed a required field, or their search query was formatted illegally. Do not use this for server crashes.
Usage Examples
Section titled “Usage Examples”import { HttpException, post } from 'shokupan';
export const createUser = post('/users', (req) => { // Basic input validation if (!req.body || !req.body.email) { throw new HttpException('Missing required field: email', 400); }
// ... create user});import { Controller, Post, Body, HttpException } from 'shokupan';
@Controller('/api')export class UserController { @Post('/users') createUser(@Body() body: any) { // Validation check if (!body.email) { throw new HttpException('Missing required field: email', 400); }
// ... create user }}import express from 'express';const app = express();
app.post('/users', (req, res) => { // Validate request body if (!req.body?.email) { return res.status(400).json({ error: 'Missing required field: email' }); }
// ... create user});import { Controller, Post, Body, BadRequestException } from '@nestjs/common';
@Controller('users')export class UserController { @Post() createUser(@Body() body: any) { // Usually Nest uses DTOs and ValidationPipes for this! if (!body.email) { throw new BadRequestException('Missing required field: email'); }
// ... create user }}import Koa from 'koa';const app = new Koa();
// Assuming you're using koa-bodyparserapp.use(async ctx => { if (ctx.path === '/users' && ctx.method === 'POST') { const body = ctx.request.body;
if (!body || !body.email) { ctx.status = 400; ctx.body = { error: 'Missing required field: email' }; return; }
// ... create user }});