1

On the moment, I am at the point where the client can send requests to the server, have the server perform actions and send back a response to the client.

For exemple, when I want to change a password, I verify the old password in my database. If it matches, the password is updated and I send a 200 response like so :

sendResponse("User's Password Updated", exchange, 200, true);
which is read in chrome's network tab as : { "response" : "User's Password Updated"}

If the old password is wrong (can't verify the user's identity), it sends the following message : sendResponse("User's password could not be verified", exchange, 200, true);
which is read in chrome's network tab as : { "response" : "User's password could not be verified"}

Now what I want to do is to perform a different action on my client for each action. For this exemple, let's log a message.

I want to have something like that (pseudocode):

if (response === "User's password could not be verified")
   console.log('User's password could not be verified');
else if ( response === "User's Password Updated")
   console.log('User's Password Updated');

keep in mind this is my first time using http, and I am pretty happy with myself being able to display the different responses in the network tab

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kass224
  • 89
  • 11
  • 1
    I don't see why it matters where `response` comes from. It's just a String (or Response object), and you have a conditional, which is the correct way to "do actions depending on some condition" – OneCricketeer Sep 17 '21 at 14:43
  • yeah but what do I write to check the response ? – Kass224 Sep 17 '21 at 14:44
  • `===` is fine to check **exact text**. If you have more specific text that can change, then you'd want to use regex or otherwise parse the string – OneCricketeer Sep 17 '21 at 14:44
  • nevermind, all I want to know is what is the method that puts the response in a string, I can do the rest... – Kass224 Sep 17 '21 at 14:49
  • 1
    A [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object has a `body` attribute and a `text()` method – OneCricketeer Sep 17 '21 at 14:51
  • 1
    I think you should console log something based on the http code. It doesn't really make sense to send a 200 response when "User's password could not be verified" because it did not successfully auth the user. Rather send a `401` http resonse and check for that – user Sep 17 '21 at 14:53
  • how do I read a response code and perform an action? @user – Kass224 Sep 17 '21 at 14:55
  • 1
    `response.status` (should be a number not string). @OneCricketeer sent a good resource that allows you to check all the attributes on the response object. – user Sep 17 '21 at 14:56
  • 1
    yeah I saw it, thank you!! – Kass224 Sep 17 '21 at 15:00
  • No problem, please accept my answer :) – user Sep 17 '21 at 15:04

1 Answers1

1

As I said in my comment you should conditionally display a message based on the http response code, not message.

To do that you would modify your code to this:

sendResponse("User's password could not be verified", exchange, 401, true);

Then in your if statement you would do:

if (response.status === 401) {
   console.log('User's password could not be verified');
} else if ( response === 200) {
   console.log('User's Password Updated');
}

I suggest you read up on the response object: Response and HTTP Status Codes..

This also may help.

user
  • 1,022
  • 2
  • 8
  • 30
  • Thank you, I will do that, but my problem is that I don't know how to create the Response Object in this situation or even where to put my if statement – Kass224 Sep 17 '21 at 15:11
  • I'm not 100% sure but I'm assuming you are using the fetch api or axios to post data to your server, correct? – user Sep 17 '21 at 15:14
  • I am using HttpClient in my constructor – Kass224 Sep 17 '21 at 15:15
  • You using angular? If so: https://angular.io/guide/http#reading-the-full-response – user Sep 17 '21 at 15:20
  • I use `this.http.post(url, body, option).pipe(catchError(this.handleError(request)))` so I would like to do something similar. the handleError function is similar to the one you answered. It is just a couple of if statements. – Kass224 Sep 17 '21 at 15:22
  • I'm not that familiar with angular, but I've updated my answer with a link to a similar question to yours. – user Sep 17 '21 at 15:26