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:
What is the best practice? Is there a clever way to check for all types of success status? Or should we always hard code:
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent guidance, quick answer, thank you so much!
https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1