Learn HTTP Status Codes with HTTP Cats (2024)

If you're new to APIs, then you might only be familiar with a few HTTP status codes. To help you learn more HTTP status codes and build your first Postman Collection, let's walk through this beginner's tutorial working with the playful HTTP Cats API. And if you're allergic to cats, use HTTP Status Dogs instead.

Learn HTTP Status Codes with HTTP Cats (1)

Prerequisites

  • A Postman account
  • Introductory knowledge of APIs

What You'll Learn

  • Explain what an API client is
  • Authorize and send HTTP API requests
  • Write tests to assert the behavior of APIs is as expected
  • Use variables to store and reuse information
  • Debug and troubleshoot unexpected behavior

What You'll Build

  • A Postman Collection consisting of requests, variables, and tests.

When you're talking to an API, the server sends back an HTTP code to signal the status of your request. You might be familiar with 200 OK or 404 Not Found. But there are also rare codes not often seen in the wild.

Industry standards typically group HTTP status codes into five categories according to the following scenarios:

  • 100s Informational responses
  • 200s Successful responses
  • 300s Redirects
  • 400s Client errors
  • 500s Server errors

Despite these standards, it's still up to the developer creating the API to account for these scenarios and return a meaningful code or error message. I've encountered APIs that return 200 OK, even when things are not okay.

But when these codes are used accurately and consistently, this information from the server helps you figure out what's really going on.

Let's build our first Postman Collection

Let's build our first Postman Collection and have fun exploring the HTTP Cats API. Follow along in Postman by forking this example collection, "Learn HTTP status codes with cats", and environment to your own workspace. Or follow these steps to build your own collection from scratch.

Begin by creating a new Postman workspace. This can be a team workspace if you're planning to collaborate with someone else. But I'm using a personal workspace called Cats to organize all of my important, but private, cat-related work.

Learn HTTP Status Codes with HTTP Cats (3)Create a new personal workspace

In Postman, paste the URL https://http.cat. Hit Send. We see Postman acting like our web browser, displaying the underlying HTML returned for the HTTP Cats website.

Learn HTTP Status Codes with HTTP Cats (4)Use Postman as an API client

Let's save the request to a new Postman Collection to keep all of the API calls we'll make. Call the collection HTTP cats and the request HTTP cats website.

Learn HTTP Status Codes with HTTP Cats (5)Keep all the API calls in a collection

Take a look at the response on the bottom. The Pretty tab of the response body shows the HTML skeleton of the web page. The Raw tab shows the raw response from the server. The Preview tab looks kind of like the web page in our browser, except with JavaScript and images disabled. We'll talk about the Visualize tab a bit later on.

Learn HTTP Status Codes with HTTP Cats (6)A website has an API that returns stuff like text and images

People usually write tests under the Tests tab. But you can write any kind of code to run after Postman receives a server response. For example, let's add this JavaScript to scrape the web page returned from the server. We'll use cheerio to scrape all the hyperlinks. It's one of the libraries that comes preinstalled in the Postman Sandbox.

let webpage = pm.response.text();const $ = cheerio.load(webpage);let codes = [];$("a").each(function () { let link = $(this).attr("href"); if (link[0] == "/" && link.length > 1) { codes.push({ code: link, }); }});

Learn HTTP Status Codes with HTTP Cats (7)Code to scrape all the hyperlinks

In programming, environments help store configuration data and secrets. They're used for saving any information to be used later.

Let's create a new called http-cats and select it as the active environment. Add one more line of code to our test script to save our scraped hyperlinks as an environment variable in Postman.

pm.environment.set("allCodes", JSON.stringify(codes));

Learn HTTP Status Codes with HTTP Cats (8)Create a new environment and set an environment variable in the test script

Once you set a variable, you can get it.

Duplicate the first request within our collection and call the new request Get one cat. Delete the script under the Tests tab. Append {{statusCode}} to the end of the URL. The curly braces syntax allows you to use defined variables in the text fields. But wait, we haven't defined a variable called statusCode yet. That's up next.

Learn HTTP Status Codes with HTTP Cats (9)Use curly braces to use variables in the text fields like

{{statusCode}}

People can use pre-request scripts for setting up their main request (e.g., calculating or retrieving a variable). Let's do that now. Under the Pre-request Script tab, add this code to retrieve all of our scraped hyperlinks and remove one to be used in this request (remember to save this data as a variable so that it can be accessed in the main request):

let arrayOfCodes = JSON.parse(pm.environment.get("allCodes"));// remove first item to update the URL pathlet oneCode = arrayOfCodes.shift();console.log("Setting this status code: ", oneCode.code);pm.variables.set("statusCode", oneCode.code);// re-save the shorter arraypm.environment.set("allCodes", JSON.stringify(arrayOfCodes));

Learn HTTP Status Codes with HTTP Cats (10)Pre-request script to get a status code to use in the main request

Let's return to our first request, HTTP cats website, to make sure the code under the Tests tab is running correctly. Hit Send, and verify that an environment variable called allCodes exists and contains an array of objects with HTTP status codes.

Learn HTTP Status Codes with HTTP Cats (11)Initialize environment variable containing HTTP status codes

Then return to our second request, Get one cat, to make sure the code under the Pre-Request Script tab is running correctly. Hit Send, and verify the API returns an image of the first status code in the environment variable allCodes.

Learn HTTP Status Codes with HTTP Cats (12)Response of a single image

We can continue cycling through all the status codes that remain in allCodes by continuing to hit Send.

It can be a bit tricky to see exactly what Postman is sending to the server since {{statusCode}} isn't defined until the pre-request script runs when the request is sent. To gain more visibility into the network calls and log statements, open the Postman console:

Learn HTTP Status Codes with HTTP Cats (13)Use the Postman console for more visibility into network calls and log statements

Keep exploring with these advanced topics

We've covered a lot of HTTP-and-cats ground already 😻. If you're still hanging in there, let's think about some next possible steps.

  • Add another API to the mix: Now that you know how to pass information from one request to another, you can send a tweet or a notification about important cat-related data. Check out the Postman API Network where you'll find a ton of interesting public APIs to add to your mix.
  • Build a custom workflow: Use Postman's collection runner to run a bunch of requests in one go, instead of hitting the Send button one request at a time. Schedule a weekly cat collection run to run on Postman servers. Or check out some of the other libraries in the Postman Sandbox. We used cheerio to scrape hyperlinks, but what you can do with code is limitless.
  • Visualize a server response: Earlier in this post, we briefly mentioned the mysterious Visualize tab in the response viewer. If you know a bit of HTML and CSS, check out the Postman visualizer to understand what you can do under the Visualize tab. Or take a look at the third request in the example collection for a sneak peek.

Next Steps

Like a cat knocking glasses off the table for fun, you can use Postman to poke at your APIs just to see what happens. Now you know there's a lot more stuff you can do with Postman, so keep playing around.

If you're looking for other cat-related tech, take a look at TheCatAPI and Cat Facts API. If you're curious about Docker and Kubernetes, check out my cat URL shortener.

Learn HTTP Status Codes with HTTP Cats (2024)

FAQs

What is the 304 status code in cat? ›

The HTTP 304 Not Modified client redirection response code indicates that there is no need to retransmit the requested resources. It is an implicit redirection to a cached resource.

What is error code 504 on cats? ›

Description. The HTTP 504 Gateway Timeout server error response code indicates that the server, while acting as a gateway or proxy, did not get a response in time from the upstream server that it needed in order to complete the request.

What is the most famous error code? ›

Status Code 404 – The most common status code the average user will see. A status code 404 occurs when the request is valid, but the resource cannot be found on the server.

What is 503 cat error? ›

The HyperText Transfer Protocol (HTTP) 503 Service Unavailable server error response code indicates that the server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded.

What is the difference between status code 200 and 304? ›

In other words, a 200 status code means that the server is sending the requested resource in the response, while a 304 status code means that the server is not sending the requested resource again because the client already has a current version of the resource in its cache.

What is error code 500 cat? ›

500 - Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

What is a 400 bad request? ›

A 400 bad request error occurs when a browser sends a request to a web server that the server cannot understand or process correctly. This is an HTTP response status code in the 4XX range.

How do I choose HTTP status code? ›

Choose the appropriate HTTP status code for each response, based on the nature of the response. For example, use the 200 OK status code for successful requests, the 201 Created status code for requests that result in the creation of a new resource, and the 400 Bad Request status code for requests with malformed data.

What is the difference between 400 and 500 error? ›

In general, 400 errors tell us something is wrong with the request — the syntax, for example. And 500 errors are usually generic errors, usually returned by the server when no other error code is suitable.

What is the difference between 504 and 502? ›

These two common HTTP status codes sound similar, but their meanings are different. A 502 bad gateway means that the server acting as the gateway received an invalid response from the main server. A 504 gateway timeout means that the server acting as the gateway didn't receive a response at all from the main server.

What is the difference between HTTP 504 and 503? ›

HTTP 503: This error indicates that one or more cluster nodes don't have enough capacity to handle the request. For more information, see How do I resolve an HTTP 503 Service Unavailable error in Amazon OpenSearch Service? HTTP 504: This error happens when a request fails to complete within the idle timeout period.

What is HTTP 200 vs 201 vs 202? ›

Use HTTP status code 200 for successful requests that retrieve or update a resource. Use HTTP status code 201 for successful requests that create a new resource on the server. Use HTTP status code 202 for requests that have been accepted for processing but the processing has not yet been completed.

What is the status code for 200 to 500? ›

The most common HTTP statuses include 200 OK, 301 Moved Permanently, 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found, 500 Internal Server Error, and 503 Service Unavailable.

What is status code 300 to 400? ›

200s: Success codes returned when browser request was received, understood, and processed by the server. 300s: Redirection codes returned when a new resource has been substituted for the requested resource. 400s: Client error codes indicating that there was a problem with the request.

Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 6067

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.