2

I am trying to send a json response back to the application with a status code. This what I tired but didn't work.

if (validation.fails()) {
    console.log(validation.messages())
    return response.json(validation.messages(),false,401) 
    // or return response.json(validation.messages(),401)
   // It always sends 200 status code 

}
Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95

2 Answers2

2

Found the solution. I need to use like this return response.status(401).json(validation.messages())

Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95
0

One better solution is to use descriptive methods: https://adonisjs.com/docs/4.1/response#_descriptive_methods

Example:

response.unauthorized('Login First')
Arash Younesi
  • 1,671
  • 1
  • 14
  • 23