Updating RESTfully value with Post

I'm new to REST, so forgive me if this is a stupid question.

So, I have a client resource. The client has a lot of loans. So, I believe that the URL for receiving customer loans will be

client / 21 / loans

(where 21 is the customer identifier)

Now, how can I add to loans if I do not have the full amount of loans? For instance. the client has 10 credits, and I want to add 5. As I understand it, if I use the message, I would do something like:

customer / 21 / credits? amount = 15 (is that even right?)

However, what if I just want to add to existing loans? That is, I want to send 5 credits and say add them to what the client currently has? Am I defining some phantom resource like addedCredits?

client / 21 / addedCredits? Amount = 5

then backstage, am I just making loans + = 5?

+3
source share
4 answers

You need to determine how you are going to relate to “loans” in your system; Is it important whether you intend to define them as resources or as an attribute of your customer resource.

In the examples below, I'm going to use XML to represent resources / entities. This may work for you, but you will need to have a consistent way of representing your resources in requests and responses - this will help you avoid using query parameters (e.g. http://example.com ? Foo = bar) to determine the data that belongs request body.

Several ways to present loans:

  • If the "credit" attribute of your "customer":

    <customer id="21"> <balance>10</balance><!-- aka credit --> </customer> 

    Then you could just GET the client, update the loan / balance with your client, and then PUT <customer> return to /customer/21 .

  • If the "credit" own resource :

    You can send the following message to /credit :

     <credit> <dateApplied>2009-10-15 15:00:00</dateApplied> <customer href="/customer/21"/> <amount>5</amount> </credit> 

    Or you can send the following message /customer/21/credits (provided that the URI is a list of all <credit> applied to the client):

     <credit> <dateApplied>2009-10-15 15:00:00</dateApplied> <amount>5</amount> </credit> 

    This will add a new <credit> to the existing list. It also eliminates the need to provide a <customer> in essence, since it is already present in the URI.

+2
source

I would use the same URL.

POST to customer/21/credits with a POST variable with the name extraCredit set to 5. It is assumed that POST is for annotating existing resources (or creating subordinate resources). There is no reason why you need a new URL.

If an individual loan is a resource of your system that deserves its own URL, then the response URL from POSTing to customer/21/credits should include the URL of the new loan, for example. customer/21/credit/12 .

You can define an XML representation of POST loans for customer/21/credits , but I would not find this worthwhile in this simple example. REST payloads do not have to be XML.

A url like customer/21/addedCredits?amount=5 doesn't make sense to me because it really doesn't identify the resource. If someone issues a GET on customer/21/addedCredits?amount=5 , what would you return to them?

The only thing you should do is not change the state of the client’s resource when someone receives a URL, for example customer/21/addedCredits?amount=5 . Since the name of your question confirms that you will need to use POST, you are probably aware of this. GET must be safe, which means that GET should not change the state of the resource.

+1
source

Ultimately, the implementation is up to you. If URI request parameters are usually not approved, this does not mean that you cannot use them. Personally, I would make a message URI something like:

client / 21 / loans / add / 5

but nothing says that you cannot do something like what you have or:

customer / 21 / credits / add? Value = 5

0
source

To get started, using URI request parameters should be a "bad smell" for you. Secondly, you need to look at the definition of some types of mime that your clients and server can talk to, for example, with your example:

If I get GET on client / 21 / credits, I can get this document:

Content-type: application / vnd.creditstore + xml

 <credits> <user>21</user> <credits>10</credits> <a href="/customer/21/credits/add" rel="add">Add credits to this account</a> </credits> 

This tells the client who understands your vocab that if they want to add credits to this user, they need to send something via this link. This is HATEOAS (God, I hate this abbreviation, I probably wasn't even mistaken).

Now all this is completely out of line with my head, and I probably hit this XML example, but it should make you think in the right direction.

-one
source