I am designing an API with express/node, and I would like to know if one does it need to explicitly set the headers for request and/ or response or is that automatically handled? As I just read the following for RESTFUL API best practices:
Use HTTP headers for serialization formats
Both, client and server, need to know which format is used for the communication. The format has to be specified in the HTTP-Header.
Content-Type defines the request format. Accept defines a list of acceptable response formats.
https://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/
However I am not sure what that truly means in practice.
app.delete('/LimeLINE/api/v1/users/delete/:boolean', (req, res) => {
let inactive;
try {
inactive = req.params.boolean
} catch (e) {
log('e', 'app.delete(/LimeLINE/api/v1/users/delete/:boolean - e - 291 : ' + e)
return res.status(500).send({
message: "INTERNAL SERVER ERROR"
})
}
//res.setHeader('Content-Type', 'application/json');
user.deleteInActive(inactive, res)
})
If you're using Express then it does a lot of work already for you. So, you don't need to specify content-type in header as it already provides methods and handles most of the header portion ex : res.json() ; It already sets the content-type to json.
In case of handling errors, you may specify "status" property and set HttpStatus to UNAUTHORIZED , INTERNAL_SERVER_ERROR, BAD_REQUEST , etc.
so you just need to use the inbuilt express functions which does all the work for you.