Coding Exercise: Mock HTTP Status Code

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will share code that is useful for solving problems. Talk code and dream code.
Useful for purging backup/temporary/cache files after a certain period of time.
Most AI agents that support scheduled tasks have two sources of instruction when a cron job fires: The cron prompt — the task-specific instructions you wrote when scheduling the job The skill — a re

TL;DR Scarp draw result, host on GitHub, retrieve it like API at damacai.hongineer.com. Data Scraping I have previously shared a technique for scraping Da Ma Cai draw result. Now I finally have time t

This is a Bash script that check if the replica (slave) health status. If there is an issue, it will exist with non-zero status. This is useful when used with monitoring tools like [Monit](https://mmo

chmod is a command-line utility used to change the access permissions of file system objects such as files and directories. It allows you to control who can read, write, and execute those objects. Changing Permissions Using Octal Notation Example Usa...
chown changes the ownership of files and directories. This involves modifying the user and/or group associated with a file, determining who has access and permissions. Changing File Ownership to a Specific User Example Usage: chown user file.txt What...
Sometime I need to test how an application response to certain HTTP status code. Testing handling of non-200 status is quite important for API integration. I came across a handy tool at https://httpstatus.io/mocking-data. Unfortunately it does not support mocking of non-standard status code, e.g. "499".
So I resort to writing and hosting a simple service myself. This is how it works:
# RFC 2324 defined code
curl -i https://http.mrleong.net/418
# Custom status code
curl -i https://http.mrleong.net/499
Understand the meaning of each status code. (Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
Whenever possible, use the standard status codes. Making sure the situation matches the status reason. For example, an API client query for an unregistered customer ID, 404 would be a suitable code.
If you really need to define custom status code, stick to 2xx, 3xx, 4xx, 5xx. Most of the time you only need 200, 4xx, and 5xx.
Use 4xx for client-side errors (e.g. authentication, authorization, validation failure)
Use 5xx for server-side error (e.g. unable to connect 3rd party services, DB issue)
My service does not work well with 1xx codes. If anyone has any suggestion for improvement, feel free to discuss it in the comment section.