?
Solved

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

Posted on 2016-11-22
3
Medium Priority
?
111 Views
Last Modified: 2016-11-22
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?
0
Comment
Question by:Eugene Vassiliev
3 Comments
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 41898382
1
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 2000 total points
ID: 41898388
Hi mate,
ideally, you shoud know exactly what to expect from the API so, for me, the more explicit on the validation, the better.
This means that, for me, the following is the best:
if (res.status === 200 || res.status === 201)

Open in new window


As you can see here, the 200 range is for successes, so if you don't really care about which kind of success it was, you can generalize:
if (res.status >= 200 && res.status < 300)

Open in new window


Depending on how you are doing the AJAX requests, you can also use the success handle to perform an action on success:
var jqxhr = $.ajax( "example.php" )
  .done(function() {
    alert( "success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "complete" );
  });

Open in new window

Example taken from here.

Cheers,
Alex
0
 

Author Closing Comment

by:Eugene Vassiliev
ID: 41898400
Excellent guidance, quick answer, thank you so much!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question