Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

172
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!