Hkm Sadek
Hkm Sadek

Reputation: 3209

How to send json response back in adonis js with a status code like 401?

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 

}

Upvotes: 2

Views: 2938

Answers (2)

Arash Younesi
Arash Younesi

Reputation: 1760

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

Example:

response.unauthorized('Login First')

Upvotes: 0

Hkm Sadek
Hkm Sadek

Reputation: 3209

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

Upvotes: 2

Related Questions