Solved

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

Posted on 2016-11-22
3
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

733 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