Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
How to test post route in fastify?

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)
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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)
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda