How can i use content negotiation when validate data? #1269
-
Im using windows 10 with wsl 2 and just start project with Im using insomnia, but its uses this part of code. #!/usr/env bash
curl -X POST localhost:3333 \
-H 'accept: application/vnd.api+json' -H 'content-type: application/json' -d '{"username":""}' Route.post('/', async ({ request }) => (await request.validate(class {
public schema = schema.create({username: schema.string()})
})))
// {"errors": [{"rule": "required", "field": "username", "message": "required validation failed"}]} But, if accept part is hardcoded then it works. Route.post('/', async ({ request }) => {
request.request.headers.accept = 'application/vnd.api+json'
await request.validate(class {
public schema = schema.create({username: schema.string()})
})
})
// {"errors": [{"code": "required", "source": {"pointer": "username"}, "title": "required validation failed"}]} What is im doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @nandordudas! 👋 If you want to have JSON API format back you need to send the Note that you are using the validator wrong. |
Beta Was this translation helpful? Give feedback.
-
Thanks!
Yes, this is a very bad example! Only for demo, no one use it 😃 |
Beta Was this translation helpful? Give feedback.
Hey @nandordudas! 👋
If you want to have JSON API format back you need to send the
Accept
header everytime.You can also force the
Accept
in your configurationconfig/app.ts
.Note that you are using the validator wrong.
You shouldn't pass an anonymous class.
https://preview.adonisjs.com/guides/validator/usage