简体   繁体   中英

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 

}

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

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

Example:

response.unauthorized('Login First')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM