0

I'm using fastify-cli. I already created a routed post. Its work normally when I call it from Postman, but not working when I call it from test command. It says FST_ERR_CTP_INVALID_MEDIA_TYPE. Where is my mistake?

const UserCreate = {
    email: 'email@email.com',
    password: bcrypt.hashSync('password', salt),
    name: 'User Fullname',
    username: 'cooluser',
    initial: 'U',
    roleId: 1,
    status: 'active'
}


test(`create User`, async (t) => {
    const app = build(t)

    const res = await app.inject({
        url: '/administrator/user',
        method: 'POST',
        payload: JSON.stringify(UserCreate),
        headers: {
            'Accept': 'application/json'
        }
    })
    console.log(`res.payload`, res.payload)
    t.equal(JSON.parse(res.payload).code, 200)
})
Herman
  • 67
  • 2
  • 5

1 Answers1

0

Solved. I just need to change payload: JSON.stringify(UserCreate) to body: UserCreate. So I don't needto stringify usercreate object to text. and also I get data from body, so I also need to change to body instead of payload.

test code should be like this:


test(`create User`, async (t) => {
    const app = build(t)

    const res = await app.inject({
        url: '/administrator/user',
        method: 'POST',
        body: UserCreate,
        headers: {
            'Accept': 'application/json'
        }
    })
    console.log(`res.payload`, res.payload)
    t.equal(JSON.parse(res.payload).code, 200)
})
Herman
  • 67
  • 2
  • 5