简体   繁体   中英

How can I prove HTTP POST Method is not Idempotent?

Is there anyway to prove Http POST Method is not idempotent from below sample code

Sample Code:

@RequestMapping(value = "/event", method = RequestMethod.POST, consumes = "application/json")
public void printEvent(@RequestBody Request request, HttpServletRequest httpRequest) throws InterruptedException {
    System.out.println("HttpRequest: "+httpRequest.getHeader("session")+" & Request: "+request.toString());
    Thread.sleep(5000);
    System.out.println("HttpRequest: "+httpRequest.getHeader("session")+" & Request: "+request.toString());
}

Being NOT idempotent is a "contract" that the HTTP method like POST agrees upon.

The developer writing the back end code can certainly break the contract by making a POST request idempotent.
The sample code provided does exactly that. The controller only does one thing - returning a view called "Greetings from Spring Boot!"

The same idea applies to other HTTP methods as well.
If the developer writing the backend controller handling GET request in a way that changes the database (create an entity in a database in the GET controller directly), the GET request will not be idempotent anymore even though the contract says otherwise. It is absolutely a bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM