How to simulate HTTP status code?

HTTP status codes are crucial indicators of whether an HTTP request has been successfully completed or encountered errors. While it is common to test webpages when APIs return success status codes, it is equally important to consider how webpages behave in response to different status codes, especially error codes.

In this article, we will explore a method to simulate and change HTTP status codes without modifying the codebase. We will achieve this using Requestly, an open-source developer tool.

Use Cases

  1. Testing how a website behaves on error scenarios.
  2. Testing Code Paths triggered by specific HTTP Status Codes in External Services

Example for testing

Let’s say you want to see what happens if the request fetching the Hero Section (Hotels, Resorts, Hostel & More) of agoda.com fails. Let’s see how the app behaves by simulating the Response status code using Requestly.

Hero section when the request succeeds

Using the Requestly Desktop app

  1. Download and Install the Requestly desktop app.
  2. Open the app, and click Connect Apps.
  3. Choose the browser you want to launch and open agoda.com
  4. In the Network Traffic table in the Requestly app, search for GetHomeContents in the Network Logs.
  5. Right-click on the request you want to change and then select Modify Response Body
  6. Enter the Simulated Response Status code (404)you want to test.
  7. Click Create Rule and reload the website again to see the updated results.
Hero Section has no text and background image after we modified the status code to 404

//Video -GIF

Using Requestly browser extension:

If you wish to have a more lightweight approach, albeit not a straightforward one, you can install Requestly’s browser extension to modify the JS files of a website.

  1. Install the Requestly browser extension.
  2. Go to HTTP Rules, and create a Modify API Response Rule.
  3. Enter the source URL condition (URL Contains /GetHomeContents) you want to match.
  4. Enter the status code you want to simulate (404 in our case).

5. Click Create Rule and reload the website again to see the updated results.

Note:

  • Only requests triggered by XHR/fetch can be modified by the browser extension. For other types of requests, use Desktop App.
  • Due to technical constraints, Response Modifications will not appear in the browser network dev tools.

This article was written by:

Picture of Sagar Soni

Sagar Soni

Sagar is the co-founder and CTO of Requestly. When he's not busy architecting the next big thing in software development, you'll likely find him behind the wheel, cruising the streets and enjoying the thrill of the drive. For Sagar, it's all about the code by day and the open road by night.

Share this article:

You may also like