Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 338
ppoliani Avatar asked Dec 22 '14 19:12

ppoliani


People also ask

How can I get status code from HTTP status?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error.

How do I set HTTP status code in response?

To set a different HTTP status code from your Servlet, call the following method on the HttpServletResponse object passed in to your server: res. setStatus(nnn); where nnn is a valid HTTP status code.

What is remote method in LoopBack?

A remote method is a method of a model, exposed over a custom REST endpoint. Use a remote method to perform operations not provided by LoopBack's standard model REST API. Note: The easiest way to define a remote method is by using the command-line remote method generator.

What HTTP status code will you return to a client running into rate limiting?

The HTTP 429 Too Many Requests response status code indicates the user has sent too many requests in a given amount of time ("rate limiting").


1 Answers

You can return any status code just like you would in ExpressJS.

...
res.status(400).send('Bad Request');
...

See http://expressjs.com/api.html

like image 177
superkhau Avatar answered Oct 19 '22 05:10

superkhau