Return to site

🌐🛑 REST GET is for Fetching, NOT Editing

A GET endpoint should retrieve a resource; not secretly modify it.

It may look convenient to read a page-view counter and increment it in the same request, but this breaks an important HTTP rule: GET should be safe and free of intended state changes.

🔸 TLDR

▪️ Use GET to retrieve data.

▪️ Use POST for a non-idempotent action such as incrementing a counter.

▪️ Separating reads from writes makes APIs safer and more predictable.

Section image

🔸 WHAT IS REST?

REST, or Representational State Transfer, is an architectural style for designing APIs around resources.

Each HTTP method communicates an intention:

▪️ GET retrieves a resource.

▪️ POST submits an action or creates data.

▪️ PUT replaces or updates a resource predictably.

▪️ DELETE removes a resource.

🔸 WHAT IS GET?

GET requests a representation of a resource.

It should be:

▪️ Safe: no intended business-state modification.

▪️ Idempotent: repeating it has the same intended effect.

▪️ Cache-friendly: clients and intermediaries may reuse responses.

❌ WRONG: GET READS AND INCREMENTS

@GetMapping("/articles/{id}/views")
public long getViews(@PathVariable Long id) {
    Article article = repository.findById(id).orElseThrow();

    article.setViews(article.getViews() + 1);
    repository.save(article);

    return article.getViews();
}

This GET changes the database. A browser refresh, crawler, cache revalidation or automatic retry could unexpectedly increase the counter. The endpoint looks like a query but behaves like a command. ⚠️

✅ GOOD: SEPARATE THE READ AND WRITE

@GetMapping("/articles/{id}/views")
public long getViews(@PathVariable Long id) {
    return repository.findById(id)
        .orElseThrow()
        .getViews();
}

@PostMapping("/articles/{id}/views")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void incrementViews(@PathVariable Long id) {
    articleService.incrementViews(id);
}

GET only retrieves the current value. POST explicitly performs the non-idempotent increment. Clients, monitoring tools and developers can now understand each endpoint from its HTTP method. ✅

🔸 WHY SEPARATE THEM?

HTTP infrastructure does not treat GET like a write operation.

Browsers, bots, proxies and clients may prefetch, repeat or cache it. When reading and writing are mixed, an innocent request can cause accidental updates, misleading analytics and harder debugging.

Clear HTTP semantics also improve:

▪️ API readability 🧭

▪️ Cache behavior ⚡

▪️ Security rules 🔐

▪️ Testing and monitoring 🧪

▪️ Retry safety 🔁

🔸 TAKEAWAYS

▪️ A query should not secretly become a command.

▪️ Use GET to fetch the page-view count.

▪️ Use POST to record or increment a view.

▪️ Make state changes explicit in your API contract.

▪️ Predictable APIs are easier to operate and maintain.

Your endpoint name tells part of the story. Its HTTP method tells the intention.

#REST #RESTAPI #HTTP #SpringBoot #Java #BackendDevelopment #APIDesign #WebDevelopment #SoftwareEngineering #CleanCode

Go further with Java certification:

Java👇

Spring👇

SpringBook👇

JavaFullstackBook👇