Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move a REST resource?

Tags:

rest

I'm trying to move a resource from /buckets/1 to /buckets/2 such that:

Initial state

  • /buckets/1 = foo
  • /buckets/2 = HTTP 404

Final state

  • /buckets/1 = HTTP 301 to /buckets/2
  • /buckets/2 = foo

What's a RESTful way of asking the server to move a resource in this manner?

like image 602
Gili Avatar asked Apr 05 '11 17:04

Gili


People also ask

How do I transfer data from REST API?

REST API POST Example To send data to the REST API server, you must make an HTTP POST request and include the POST data in the request's body. You also need to provide the Content-Type: application/json and Content-Length request headers. Below is an example of a REST API POST request to a ReqBin REST API endpoint.

What is resource path in REST API?

Resource Path (request target)The path to the resource (object) to be acted upon. The ID of the resource must be provided in the path. For example, the following resource path identifies a specific transaction (resource) in our database: Copy. https://apitest.cybersource.com/pts/v2/payments/

What is an example of a REST resource?

Resources are the basic building block of a RESTful service. Examples of a resource from an online book store application include a book, an order from a store, and a collection of users. Resources are addressable by URLs and HTTP methods can perform operations on resources.

How do I manage RESTful API?

Access to the direct management API Management REST API must be granted before calls can be successfully made. Navigate to your Azure API Management instance in the Azure portal. Select Management API from the Deployment + infrastructure section of the menu on the left. In Enable API Management REST API, select Yes.


1 Answers

Answering my own question:

  • For the sake of this discussion let's assume that we are storing "balls" in buckets
  • The first thing to notice is that a ball's life-cycle is not determined by its containing bucket (moving a ball from one bucket to another does not delete the old ball). As such we should promote balls to a top-level resource: /balls
  • REST seems to work best in terms of symbolic links as opposed to inline values, so instead of GET /buckets/1 returning the value of the ball in the bucket let's have it return the URI of the ball instead.

We can then move balls as follows:

(examine original state)
GET /buckets/1: "balls = {'/balls/1'}"
GET /buckets/2: "balls = {}"
GET /balls/1: "bucket = /buckets/1"

(move ball into bucket #2)
PUT /balls/1: "bucket = /buckets/2"

(examine new state)
GET /buckets/1: "balls = {}"
GET /buckets/2: "balls = {'/balls/1'}"
GET /balls/1: "bucket = /buckets/2"

End-result: the ball's identity remains consistent as it moves across buckets and (most importantly) this operation is atomic.

like image 119
Gili Avatar answered Sep 22 '22 03:09

Gili