Defining your endpoints and responding to requests in your RESTful API

Defining your endpoints: use plural

  • POST /users: creates a new user
  • GET /users: reads a list of users
  • GET /users/123: reads user with ID 123
  • PUT /users/123: updates user with ID 123
  • PATCH /users/123: partially updates user with ID 123
  • DELETE /users/123: deletes user with ID 123

Responding to GET requests: do not use envelopes

{
metadata: { ... },
data: { ... }
}
  • It makes the output a bit harder to read for the client
  • Envelopes are, in the vast majority of cases, not necessary
  • Metadata can, if needed, be returned in HTTP headers

Responding to POST, PUT and PATCH: return the resource representation

  • A call to a POST, PUT or PATCH request may make modifications not only on the fields that were changed as part of the request, but also to other fields (for example, the created_at and updated_at timestamps)
  • Returning the resource representation in the response will save one HTTP request, because the client will not need to do a GET request on the modified or newly created resource to get the new resource representation.

--

--

--

Développeur web et passionné de finances personnelles

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

This week in DevOps #51 — GitOps Issue #33

A Simple Guide to Making Websites with Bootstrap 4 (pt III) 🌖

Process sounds easy in Python

Arbitrary Parenthesization

Create a full crud API in Laravel - unit tests part (2)

WHY SD-WAN IS IMPORTANT FOR YOUR BUSINESS IN 2021

Genesys Cloud Chat Transcribe

Picking a Random Element from an array in Ruby

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Guillaume Viguier-Just

Guillaume Viguier-Just

Développeur web et passionné de finances personnelles

More from Medium

What is JWT. How to implement JWT based authentication in NodeJs?

How to implement JWT based authentication in NodeJs

IS-A and HAS-A

Are you ready to Mongo? The benefits of using MongoDB

How to Hire Node JS Developers: Under the Microscope

How to Hire Node JS Developers: Under the Microscope