How do I return specific http status codes from a remote method in loopback?

ppoliani picture ppoliani · Dec 22, 2014 · Viewed 9.4k times · Source

I would like to know if there is a way to returns a specific HTTP status code from within a remote method.

I can see that there is a callback function which we can pass an error object, but how do we define the HTTP status code?

Answer

Juan Acosta picture Juan Acosta · Aug 10, 2016

If you wish to use an HTTP status code to notify of an error, you can pass an error in the remote methods callback method:

var error = new Error("New password and confirmation do not match");
error.status = 400;
return cb(error);

You can find more information about the error object here: Error object

If you wish to just alter the HTTP response status without using an error, you can use one of the two methods defined by either #danielrvt or #superkhau. To obtain the reference to the request object mentioned by #superkhau, in your method registration you can define an additional argument that will be passed to your remote method. See HTTP mapping of input arguments