How to Simulate Long HTTP Requests

By  on  

It happens less frequently these days but there are times when we need to accommodate for a HTTP request timing out. The service could be down, under heavy traffic, or just poorly coded, or any host of other issues.

Whenever I need to simulate a long HTTP request, I use a bit of PHP to make it happen:

<?php
        // Don't resolve this request for 5 seconds
        sleep(5);
        
        // A generic response
        echo 'This is the response!';

        // ... or hit a URL to make the case more realistic
        echo file_get_contents('https://website.tld/endpoint');
?>

With that script created, I make PHP start a server so I can make the request locally:

php -S localhost:8000

Now I can hit http://localhost:8000 and get the long request I want!

There are a number of ways you can accomplish these long form requests but this has always been a favorite of mine!

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    Use Elements as Background Images with -moz-element

    We all know that each browser vendor takes the liberty of implementing their own CSS and JavaScript features, and I'm thankful for that. Mozilla and WebKit have come out with some interesting proprietary CSS properties, and since we all know that cementing standards...

  • By
    MooTools-Like Element Creation in jQuery

    I really dislike jQuery's element creation syntax. It's basically the same as typing out HTML but within a JavaScript string...ugly! Luckily Basil Goldman has created a jQuery plugin that allows you to create elements using MooTools-like syntax. Standard jQuery Element Creation Looks exactly like writing out...

Discussion

  1. That’s cool! Thanks for the tip.
    I could see having it take a query param to set the sleep time arbitrarily for different scenarios you’re simulating.

  2. Charlie

    Thanks David always love your content.
    Although in this particular case i fail to understand a practical use, could you share an example?

    thanks !

  3. Dima

    And here is concise way to do it in NodeJs, the server will wait for 3 seconds before response:

    const http = require('http')
    
    const server = http.createServer((req, res) => {
      setTimeout(() => {
        res.writeHead(200)
        res.end('Hello, World!')
      }, 3000)
    })
    server.listen(8080)
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!