RESTful API中GET动词的幂等性:时间相关接口实现疑问
Great question—this is a super common point of confusion when wrapping your head around HTTP method rules! Let's break this down clearly.
First, let's reset what idempotency actually means for HTTP GET requests: it's about the server's resource state, not the exact content of the response. The core rule is: making the same GET request multiple times must not change any resources on the server (no side effects like updating a database, modifying a file, or altering configuration).
Your example GET /get-time-now/ fits this perfectly. Every time you call it, the server doesn't create, update, or delete any resources—it just generates a response based on the current system time. The server's state remains identical before and after every call, so it's fully idempotent.
The confusion usually comes from mixing up two distinct concepts:
- Idempotency: No impact on server resources, regardless of how many times you run the request
- Static response: The same exact content returned every time
HTTP doesn't require GET responses to be static—only that the request itself doesn't alter the server. Think of other similar valid cases:
- A GET API that returns real-time stock prices: responses change constantly with market activity, but no server resources are modified
- A GET endpoint that shows whether a user is currently online: the response updates as the user's status changes, but the request doesn't trigger any changes on the server
So the quick test for your GET API: if running it 100 times leaves the server exactly as it was before the first call, it's idempotent—even if every response looks different.
内容的提问来源于stack exchange,提问作者Humam Helfawi

