Michael Bissell
Michael Bissell

Reputation: 1208

What verb and payload to set an API resource to 301 Moved?

Say I have two objects in a RESTful API:

 /resource/blue
 /resource/red

Now I want to combine the two of them and point them to a new object

/resource/orange

When I go to /resource/blue or red I want to receive a response like

HTTP/1.1 301 Moved Permanently
Date: Thu, 10 Mar 2016 21:57:47 GMT
Location: /resource/orange

So far so good... we can set that pragmatically in Spring based on data in Mongo. But what would be the best RESTful request to make to change my blue resource into a 301 redirect to my new orange resource? Should it be...

PUT /resource/blue
{"movedto" : "/resource/orange"}

Or

DELETE /resource/blue
{"movedto" : "/resource/orange"}

Or something else?

Upvotes: 0

Views: 219

Answers (2)

ChatterOne
ChatterOne

Reputation: 3541

This really looks like a PUT, because you're updating the resource (its location), and moving it twice should have no effect (you get the 301/303), because it's already there.

If you used a DELETE twice on the same resource, the call would try to delete a resource that's no longer there (it's been moved), so it should actually return a 404.

Also, by definition the DELETE verb:

SHOULD NOT indicate success unless, at the time the response is given, it intends to delete the resource or move it to an inaccessible location.

And the location where you're moving the resource is not going to be inaccessible.

Upvotes: 2

TheWhiteRabbit
TheWhiteRabbit

Reputation: 15768

In this case you can use HTTP 303 (see other) instead of HTTP 301 (moved)

Response

HTTP/1.1 303 See Other
Location: http://your.domain/resource/orange

Hope this helps

Upvotes: 1

Related Questions