2

I use axios to post username and password to express backend. In the backend I check whether the username already exists, if the username already exists I send json to the frontend. I tried sending res.status(409).json({username_exists: true}), but I got an error. Then I tried sending exactly the same response but with the status code 200 and it worked.

Why does status(409) give an error?

I read here on stackoverflow that the status code should be 409 if the username already exists, which status code to send so that the frontend receives the json?

Jack774
  • 21
  • 2
  • Define "got an error". A 400+ is an error by definition--are you asking about how to process error status code with Axios? – Dave Newton May 18 '21 at 18:08
  • @DaveNewton This is the error code: Error: Request failed with status code 409 – Jack774 May 18 '21 at 18:10
  • Is it possible to send json with an error status code? @DaveNewton – Jack774 May 18 '21 at 18:13
  • Yes, because that is what happened. I'm asking what specifically you want to be able to do, e.g., Axios defaults to erroring on... errors. You can update the `validateStatus` function to work around this. (There are other ways as well--searching the web for something like "axios error access response" will be helpful.) – Dave Newton May 18 '21 at 18:13
  • 2
    I would like to point out this showing errors like this is a big security issue, as outlined here: https://stackoverflow.com/a/32531069/1710359 – gear4 May 18 '21 at 18:14
  • I know, but I don't have any other way to tell the user that the username is taken. Is there any other way to do it? @gear4 – Jack774 May 18 '21 at 18:18
  • 1
    Axios treats all 4XX codes as errors (because they are) To improve error handling, see https://stackoverflow.com/questions/49967779/axios-handling-errors – Charlie Bamford May 18 '21 at 18:22
  • @Jack774 you should be sending a generic error (400 or whatever) that something is wrong (never be specific about something being incorrect) – gear4 May 23 '21 at 21:26

0 Answers0