How to work with combined objects / resources in a web application and in a RESTful API?

I have a web application in which there are many pages with the URL of the form / object _type / object_id / that show this information about objects. I also have a RESTful API returning a JSON / XML representation of my objects (in / api / object_type / object_id /). (My objects are laws and bills, but I think this question is pretty general).

From time to time, I find that two or more of my objects are actually duplicated and describe the same object in the real world. Let's say that they were / Bill / 111 / and / Bill / 222 /. Behind the scenes, I am combining 2 objects, leaving 1 object (say, / Bill / 111 /) with all the information, and the other is โ€œemptyโ€ with only a link to another.

My question is: how can I specify merging in a web application and in an API? I do not want / Bill / 222 / to return 404, because I can have external links pointing to it, and I do not want them to be broken. Should I use 301 moved permanently? Should I return a normal page (with a status of 200), explaining that this resource was detected as a duplicate with a link to the merged? How should I work with this in the API? for example, should I list 222 in the Bills index?

+2
source share
2 answers

I think I would use 301 for this case and stop listing 222. The only reason for 301 is because some client had a URL.

+2
source

@ Darrell Miller:

I think I would use 301 for this case and stop listing 222. The only reason for 301 is because some client had a URL.

I would add that the system should accept links to / Bill / 222 and / Bill / 111 as equivalent, that is, taking into account the editing of the user / Joe / 999, indicating that / Bill / 222 is Joe's friend:

PUT /Joe/999 Content-Type: vnd.xxx+xml <name is-friend-of="/Bill/222">Joe</name> 

It should be semantically identical to make friends / Bill / 111, and indeed, after issuing the above PUT, I will not be surprised that when I get it back, I see that the link has changed to 111.

+1
source

Source: https://habr.com/ru/post/1315352/


All Articles