How to Simulate Long HTTP Requests
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!
I was inspired when I first saw Addy Osmani's original ShineTime blog post. The hover sheen effect is simple but awesome. When I started my blog redesign, I really wanted to use a sheen effect with my logo. Using two HTML elements and...
Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user. One of those simple APIs the Vibration API. The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...
It's been quite a while since I've written a PHP grabber and the itch finally got to me. This time the victim is the International Movie Database, otherwise known as IMDB. IMDB has info on every movie ever made (or so it seems). Their...
The Web Audio API allows developers to load and decode audio on demand using JavaScript. The more I evaluate awesome games for Firefox OS TVs, the more I get to learn about these APIs that I normally wouldn't touch. The following is a very basic introduction to the WebAudio API...
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.
Thanks David always love your content.
Although in this particular case i fail to understand a practical use, could you share an example?
thanks !
And here is concise way to do it in NodeJs, the server will wait for 3 seconds before response: