HTTP Methods

> GET, POST, PUT, and DELETE -- when to use which.

Every route in Brickr is associated with an HTTP method. The method tells clients what kind of operation the route performs.

Available methods

GET

Use GET to retrieve data without modifying anything on the server.

  • Fetching a list of items
  • Reading a single record by ID
  • Searching or filtering data
GET /products
GET /products/:id
GET /users?role=admin

GET requests should not have a request body. Use query parameters or path parameters to pass data.

POST

Use POST to create new data or submit information.

  • Creating a new record
  • Submitting a form
  • Triggering an action
POST /products
POST /auth/login
POST /orders/:orderId/checkout

POST requests typically include a JSON body with the data to create.

PUT

Use PUT to update an existing record.

  • Updating user profile fields
  • Changing the status of an order
  • Replacing a resource
PUT /products/:id
PUT /users/:id/settings

PUT requests include a JSON body with the updated fields.

DELETE

Use DELETE to remove a record.

  • Deleting a user account
  • Removing an item from a list
  • Cancelling an order
DELETE /products/:id
DELETE /users/:id

DELETE requests usually rely on a path parameter to identify the resource to remove.

Choosing the right method

| Action | Method | Example Path | |--------|--------|-------------| | List all items | GET | /items | | Get one item | GET | /items/:id | | Create an item | POST | /items | | Update an item | PUT | /items/:id | | Delete an item | DELETE | /items/:id | | Search | GET | /items?q=keyword | | Submit a form | POST | /forms/:formId/submit |

Path structure conventions

Follow REST conventions to keep your API predictable:

  • Use nouns for resources: /products, /users, /orders
  • Use path parameters for specific items: /products/:id
  • Use nesting for relationships: /orders/:orderId/items
  • Use query parameters for filtering: /products?category=shoes

You can have multiple routes with the same path but different methods. For example, GET /users/:id and PUT /users/:id are two separate routes.

What's next?

| Topic | Description | |-------|-------------| | Parameters | Path params, query params, and request bodies | | Creating Routes | Step-by-step route creation |