Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now