6

After a successful creation of new item in my database I send:

res.status(201).json({message:"Successfully Registered"});

On my angular front end I am able to console.log(res) and receive:

{message: "Successfully Registered"}

1) How do I get the status code on the front end? res.status returns undefined. The only response I'm getting is the JSON.

2) What would be the best way to confirm successful desired api calls? For example, when I log in and credentials are correct, should I check for a 200 and then proceed? Or send a custom JSON message and check if the message for example says "Successful login" then proceed?

Vincent Nguyen
  • 1,555
  • 4
  • 18
  • 33
  • Checking for message contents is considered a bad practice, as it might change to fix typos, better represent the outcome, add i18n, etc. Ideally, one would compare the HTTP status of the response, as some answers indicated, although it depends on how the API is designed (some add status to the response payload) - but the majority rely on HTTP status. – Rafael Leite Apr 14 '23 at 19:11

7 Answers7

9

A little bit late, but another option is to include the status in the JSON object:

res.status(201).json({message: "Successfully Registered", status: 201})

Now you can check the status in the front end doing res.status and use this to proceed with another action.

GusSL
  • 652
  • 7
  • 23
3

1- You can do res.status(200).send("You message here");

2- I would say your best option when doing the login and authenticating credentials is to create a session as such

req.session.user = req.body.username //username is the name attribute of the textfield 

and then redirect to any page you'd like/you can also set status to 200

res.status(200);
AmirHossein Rd
  • 393
  • 4
  • 13
2

I'm not familiar with Angular, but looking at the docs:

  1. See https://angular.io/api/http/Response

You'll need to do something like:

http
    .request('example.com')
    .subscribe(response => console.log(response.status));
  1. Sure, checking for 200 is fine. Typically with a REST API (inferred from what you've shown), after a login you're given back a JWT along with 200 OK. And any subsequent API all with that JWT will also yield a 200 OK along with the response body which is usually JSON.
Cisco
  • 20,972
  • 5
  • 38
  • 60
1

You should tell your angular http client that you want to get the response status. By default, angular deserialize the response body, but you can set request option.observe:'response' to do so.

postData(model: MyModel): Observable<HttpResponse<{status: string}>> {
  return this.http.post<{status: string}>(
    model, { observe: 'response' });
}

See https://angular.io/guide/http#reading-the-full-response for details.

PS: sending a { status: 'message' } is not very useful, you may return an { id } or even nothing.

Vincent GODIN
  • 467
  • 4
  • 8
0
res.status(200).json({
    status: 'success',
    results: tours.length,
    data:{
        tour:tours
    }
});

Usually, Jsend is a good choice to response, and also by convention. Absolutely you can see the 'status' in response data and the actually data you want in the data.

0

according to the angular guide, we can add observe in the options of our request.

getforgetpassword(email: string): Observable<any> {
    const url = this.gatewayUrl + `newPasswordFor/${email}`;
    return this.http.get(url, {observe: "response"});
}

using observe type as response will give total response along with request status, which you can use in your logic of controller.

Shubham Shaw
  • 841
  • 1
  • 11
  • 24
0

A bit late but did you try Response.ok

if (res.ok) {
  //do something
}
Tabea
  • 95
  • 9