Crosspost of as our verbs to perform actions upon those nouns. For example, we can use the HTTP method GET to retrieve information about /users/1, but we could use PUT to update that corresponding user's information, or DELETE to delete the user entirely.
The last thing to note about URIs is that, as with the example above, when referencing an individual resource (e.g. a single user in this case) the URI should end with the unique identifier for that resource. When referencing all resources in a given category, the unique identifier should be omitted.
https://example.com/users/1- References a particular user with anidof1
https://example.com/users- References all users regardless ofid
What Actions to Support
There are 4 main actions to support in REST, we use the acronym CRUD to remember them: Create, Read, Update, Delete. Each of these actions maps to an HTTP Method that we can use to perform that action. The mapping is as follows:
| Action | HTTP Method |
|---|---|
Create | POST |
Read | GET |
Update | PUT / PATCH |
Delete | DELETE |
All Action + URI Combinations to Support
Every REST API is really just (at a minimum) 5-6 routes. In our example, the base endpoint will be /users and we'll pretend to host it on https://example.com.
GET https://example.com/users
Action: Return all user assets (each asset is one user)
Request Body: Empty
Response Body: List of user assets (as a JSON array)
GET
Action: Adds one user asset to the collection
Request Body: All data needed to create the new user asset (no specific format required, JSON recommended)
Response Body: The newly created asset that was inserted + a unique ID (as JSON)
PUT([id]is a variable)
Action: Partially replaces just one existing user's data with the given data
Request Body: Only the data that needs updating (minus the id - no specific format required, JSON recommended)
Response Body: The newly updated asset with the matching id (as JSON)
DELETE . Setting it up is pretty simple - just a few steps required.
Install JSON Server
CODEnpm install json-server
Create a simple data store
CODE{
"users": [
{ "id": "1", "username": "gorwell", "email": "[email protected]" },
{ "id": "2", "username": "cdickens", "email": "[email protected]" },
{ "id": "3", "username": "jausten", "email": "[email protected]" },
{ "id": "4", "username": "vwoolf", "email": "[email protected]" },
{ "id": "5", "username": "sking", "email": "[email protected]" }
]
}
Start up the server
CODEnpx json-server db.json
Make an HTTP request against your local server
CODEcurl -X GET http://localhost:3000/users/1
An Easy CRUD Data Grid
A fully functioning REST endpoint can be hooked up to a data grid easily with
Final Thoughts
REST APIs come in many shapes and sizes across the web, each tailored to meet specific needs. By structuring URIs thoughtfully, choosing the right actions, and keeping versioning in mind, you can create a straightforward, flexible API that developers will find a pleasure to work with. With these foundational steps, even a quick prototype can evolve into a robust, reliable API that stands the test of time.
↗ Original-Artikel auf dev.to lesenVollständiger Original-BerichtAusführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
SOCIAL SHARE CARD GENERATOR