Edy Bourne
Edy Bourne

Reputation: 6187

Why I keep getting Bad Request sending valid JSON to an express app using routing controllers?

I have an express app that is setup using routing-controllers. There is only one controller, and it looks like this:

@JsonController('/auth')
export class AuthController {
    public authService = new AuthService();


    @HttpCode(201)
    @Post('/signup')
    async signUp(@Body() data: SignUpDto) {
        return await this.authService.signup(data);
    }
}

And the SignUpDto really doesn't have any validations on it:

export class SignUpDto {

    userId: string;

    email: string;

    password: string;

}

However, when I send a request using cURL:

curl --cacert ./.cert/cert.pem -X POST https://localhost/auth/signup -H 'Content-ype: application/json' -d '{"userId":"01GKYW1JQBCJBXAK0VJTX92C6E","email":"[email protected]","password":"A1@2e3r4"}'

I get 400 - Bad Request back:

{"name":"BadRequestError","message":"Invalid body, check 'errors' property for more info.","stack":"Error
at new HttpError (/home/user/Work/service/node_modules/routing-controllers/cjs/http-error/HttpError.js:17:22)
at new BadRequestError (/home/user/Work/service/node_modules/routing-controllers/cjs/http-error/BadRequestError.js:10:9)
at /home/user/Work/service/node_modules/routing-controllers/cjs/ActionParameterHandler.js:219:31
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ActionParameterHandler.normalizeParamValue (/home/user/Work/service/node_modules/routing-controllers/cjs/ActionParameterHandler.js:134:21)
at async Promise.all (index 0)","errors":[{"target":{"{\"userId\":\"01GKYW1JQBCJBXAK0VJTX92C6E\",\"email\":\"[email protected]\",\"password\":\"A1@2e3r4\"}":""},"children":[],"constraints":{"unknownValue":"an unknown value was passed to the validate function"}}]}

I have been stuck on this for a few hours... any pointers on what could be causing this?

Upvotes: 0

Views: 838

Answers (1)

Edy Bourne
Edy Bourne

Reputation: 6187

I found the issue.

I was using a @JsonController but when I sent the request I did not include a Content-Type: application/json header... there was a typo in the header in my request. Without the correct one, routing-controllers doesn't seem to be able to handle the request (sometimes).

Once I added, everything works now.

Upvotes: 0

Related Questions