Link to home
Start Free TrialLog in
Avatar of Eugene Vassiliev
Eugene Vassiliev

asked on

Best way to check API call was success: status 200 vs. 201 question

We have build a simple dropbox-like web application using JavaScript/NodeJS/Express. It talks to our API routes, also written in JS. The UI is based on Bootstrap + commercial UI template. We used React/Redux architecture for web front end.

We have a button that uploads a file the user has selected, by calling our our API. We noticed that often our API returns status 200 or status  201 for successful requests. Is there a reason the status is randomly 200 or 201?

We used to check for success by checking for:
if (res.status === 200)

What is the best practice? Is there a clever way to check for all types of success status? Or should we always hard code:
if (res.status === 200 || res.status === 201)
Is there a way to avoid code duplication?
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eugene Vassiliev
Eugene Vassiliev

ASKER

Excellent guidance, quick answer, thank you so much!