Solved

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

Posted on 2016-11-22
3
47 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 52

Expert Comment

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

Accepted Solution

by:
Alexandre Simões earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

840 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