Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
ASP.NET Core Basics: Endpoints  ›  Lesson

REST — Nouns in the URL, Verbs in the Method

REST 13 minA REST API names resources with URLs and uses the HTTP method and status code to say what happened to them.

REST — Nouns in the URL, Verbs in the Method

Before writing a line of ReadingRoom's API, decide its URLs. Get them wrong and you end up with /getAllBooks, /deleteBookById and /doLoan — every endpoint a special case. REST gives you a small set of patterns that clients can predict.

The rule

URLs name things; methods say what to do; status codes say what happened.

In REST, everything the API exposes is a resource with an address. A collection is a plural noun, and one item is that noun plus its id:

Method + URL Meaning Success status
GET /books list books 200 OK
GET /books/7 read book 7 200 OK (404 if missing)
POST /books create a book 201 Created + Location
PUT /books/7 replace book 7 200 OK or 204 No Content
DELETE /books/7 remove book 7 204 No Content

Related resources nest: GET /members/5/loans lists member 5's loans. Filters go in the query string: GET /books?author=Herbert.

Two more REST ideas matter from day one. Requests are stateless: each one carries everything the server needs, so any server instance can answer it. And GET, PUT and DELETE are idempotent: sending the same one twice leaves the data as sending it once would. POST is not — two POSTs make two books.

The shortcut

When an endpoint idea arrives as a sentence, pull out the noun for the URL and the verb for the method. "Delete the book with id 7" → DELETE /books/7. If you catch a verb inside your URL, move it into the method.

Turning a request into an endpointStep 1 of 4
  1. Say it in words

    "Borrow book 3 for member 5."

The trap: POST /loans/create. The word "create" repeats what POST already says.

Drills

Answer fast — trust the rule.

quick drill

Which endpoint best follows REST for "update book 7's title"?

quick drill

Which of these methods is NOT idempotent?

quick drill

Where does a filter such as "only books by Herbert" belong?

quick drill

A successful POST /books should usually answer with what?

Remember

  • URLs name things; methods say what to do; status codes say what happened.
  • Pull the noun into the URL, the verb into the method.
  • POST is not idempotent; GET, PUT and DELETE are.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDesign the loans endpoints

Design REST endpoints for ReadingRoom loans: list a member's loans, borrow a book, and return a book. Give the method, URL and success status for each, and explain why POST /returnBook is a poor design.