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!
Firefox OS is all over the tech news and for good reason: Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript. Firefox OS has been rapidly improving...
I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...
CodePen is a treasure trove of incredible demos harnessing the power of client side languages. The client side is always limited by what browsers provide us but the creativity and cleverness of developers always pushes the boundaries of what we think the front end can do. Thanks to CSS...
We've all used word processing applications like Microsoft Word and if there's one thing they've taught you it's that you need to save every few seconds in anticipation of the inevitable crash. WordPress has mimicked this functionality within their WYSIWYG editor and I use it...
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: