Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update to-many association

Having a many-to-many relationship between users and groups. I would like to know how to update this relationship with SDR. This is what I've tried so far after reading the docs.

curl -X POST -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/rest/users/5' http://localhost:8080/rest/groups/1/users

Expected result: Add user 5 to group 1.

Actual result: 405 Method Not Allowed.

curl -X PUT -H 'Content-Type: text/uri-list' -d 'http://localhost:8080/rest/users/5' http://localhost:8080/rest/groups/1/users

Expected result: Replace all members of group 1 with user 5.

Actual result: Works as expected.

curl -X PUT -H 'Content-Type: text/uri-list' -d @members.txt http://localhost:8080/rest/groups/1/users

Where the file members.txt has:

http://localhost:8080/rest/users/5
http://localhost:8080/rest/users/6
http://localhost:8080/rest/users/7

Expected result: Replace all members of group 1 with the users 5, 6 and 7.

Actual result: Only last user (in this case 7) gets added.

Could someone provide an example on how to ADD a single URI to an association?. Also if possible, how to add or replace an association with multiple URIs?

like image 642
user1387786 Avatar asked Oct 19 '22 19:10

user1387786


1 Answers

After re-reading the documentation, it does indeed say POST should add to the collection.

My experience has been to use PATCH to add to the collection.

To further the answer: You should be able to use PUT CONTENT-TYPE: text/uri-list with a content body having multiple URIs. Each URI is separated by a line break "\n"

like image 81
Brandon Brooks Avatar answered Oct 28 '22 15:10

Brandon Brooks