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!
David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...
Back in late 2012 it was not easy to find open source projects using requestAnimationFrame()
- this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...
Ever since I got my iPhone, I've been more agreeable in going places that my fiancee wants to go. It's not because I have any interest in checking out women's shoes, looking at flowers, or that type of stuff -- it's because my iPhone lets...
I'll be honest with you: I still haven't figured out if I like my MooTools teammate Guillermo Rauch. He's got a lot stacked up against him. He's from Argentina so I get IM'ed about 10 times a day about how great Lionel...
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: