Custom update actions in RESTful Services

rest api custom actions
rest api actions endpoint
rest api trigger action
rest patch
rest uses http for which of the following actions
rest api non crud operations
rest api methods
http methods

My API requires many different types of updates that can be performed by different types of roles. A ticket can have it's data updated, a ticket can be approved (which includes some information), a ticket can be rejected, a ticket can be archived (state that makes a ticket unable to be updated), etc.

I've recently started working as a backend developer and I really do not know what is the most correct approach to this situation but I've two ideas in mind:

  • A single update endpoint (e.g. /api/tickets/:id) that accepts an action field with the type of update that wants to be done to that file;
  • Multiple custom action endpoints (e.g. /api/tickets/:id/validate, /api/tickets/:id/accept, etc.)

Which one of those is the best approach to the situation when it comes to the REST architecture? If both are incorrect when it comes to REST, what would be the most correct approach? I couldn't really find any post on SO that answered my question so I decided to create my own. Thank you!

RESTFul: state changing actions, Probably the hardest thing here is that we need to send all article data back to API for updating and multiuser work could not be implemented. For instance editor� Work with custom actions and custom resources. In this tutorial, you update the function app to work as a RESTful endpoint for your custom provider. Resources and actions in Azure are modeled after the following basic RESTful specification: PUT: Create a new resource; GET (instance): Retrieve an existing resource; DELETE: Remove an existing

Really it all comes down to a matter of taste. It is often observed in the industry to have the static parameters in the URL (eg: /tickets/update, /users/add, /admin/accounts) and the variable parameters in the query (eg: IDs, messages, names). It allows to have a fixed number of endpoints.

I see you're using NodeJS so you're probably using Express and in Express getting the url parameters and the body parameters are equally easy:

// supposing you're using a JSON-based API with body-parser for JSON parsing
app.get('/some/path/:someParam', (req, res) => {
    console.log(req.params.someParam);
    console.log(req.body.someOtherParam);
    res.send();
}

Custom actions in your REST API. While working on a REST API for , What are custom actions ? REST Principles. Action in the URL. Thinking RESTFUL: subscriptions. Option 1: subscriptions as a new resource type. Option 2: subscriptions belonging to a user. Option 3: subscriptions belonging to a list. Stripe's invoice workflow provides a great example of how to represent state transitions or domain actions in a restful manner. A action on your domain model could well be represented by a modification (PUT or PATCH) to a state or status field that would trigger workflows within your own code's state machine.

REST stands for Representational State Transfer, which means that the client and the server affect each other’s state by exchanging representations of resources.

A client might GET a representation of a ticket like this:

GET /api/tickets/123 HTTP/1.1

HTTP/1.1 200 OK
Content-Type: application/json

{
    "id": 123, 
    "state": "new",
    "archived": false,
    "comments": []
}

Now the client can PUT a new representation (replacing it wholesale) or PATCH specific parts of the existing one:

PATCH /api/tickets/123 HTTP/1.1
Content-Type: application/json-patch+json

[
    {"op": "replace", "path": "/state", "value": "approved"},
    {"op": "add", "path": "/comments", "value": {
        "text": "Looks good to me!"
    }}
]


HTTP/1.1 200 OK
Content-Type: application/json

{
    "id": 123,
    "state": "approved",
    "archived": false,
    "comments": [
        {"author": "Thomas", "text": "Looks good to me!"}
    ]
}

Note how the update is expressed in terms of what is to be changed in the JSON representation, using the standard JSON Patch format. The client could use the same approach to update any resource represented in JSON. This contributes to a uniform interface, decoupling the client from the specific server.

Of course, the server need not support arbitrary updates:

PATCH /api/tickets/123 HTTP/1.1
Content-Type: application/json-patch+json

[
    {"op": "replace", "path": "/id", "value": 456}
]


HTTP/1.1 422 Unprocessable Entity
Content-Type: text/plain

Cannot replace /id.

Still, this might require more complexity on the server than dedicated operations like "approve", "reject", etc. You might want to use a library to implement JSON Patch. If you find that you need many custom actions which are hard to express in terms of a representation, an RPC architecture might suit you better than REST.

Methods, If an API has too many actions, then that's an indication that either it was The semantics of this call are like PUT inthat it updates a resource, but unlike PUT,� PUT is most-often utilized for **update** capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource. However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server.

REST is all about resources. And the state of the resources should be manipulated using representations (such as JSON, XML, you name it) on the top of stateless communication between client and server.

Once URIs are meant to identify the resources, it makes sense to use nouns instead of verbs in the URI. And when designing a REST API over the HTTP protocol, HTTP methods should be used to indicate the action intended to be performed over the resource.

Performing partial updates

You could use the PATCH HTTP verb to perform partial updates to your resource. The PATCH request payload must contain set of changes to applied to the resource.

There are a couple of formats that can be used to describe the set of changes to be applied to the resource:

JSON Merge Patch

It's defined in the RFC 7396 and can be applied as described below:

If the provided merge patch contains members that do not appear within the target, those members are added. If the target does contain the member, the value is replaced. Null values in the merge patch are given special meaning to indicate the removal of existing values in the target.

So a request to modify the status of a ticket could be like:

PATCH /api/tickets/1 HTTP/1.1
Host: example.org
Content-Type: application/merge-patch+json

{
  "status": "valid"
}
JSON Patch

It's defined in the RFC 6902 and expresses a sequence of operations to be applies to a target JSON document. A request to modify the status of a ticket could be like:

PATCH /api/tickets/1 HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json

[
  {
    "op": "replace",
    "path": "/status",
    "value": "valid"
  }
]

The path is a JSON Pointer value, as described in the RFC 6901.

When to use PUT or POST, Use PUT when you can update a resource completely through a specific resource. Note that all other requests do not have an action while POST has an action to be performed on the Please provide appropriate HTTP REST service Call/Request for below requests: Create Retrieve Update Custom content- types. Actions¶. Sometimes, it is required to expose an operation in the API that inherently is non RESTful. One example of such an operation is where you want to introduce a state change for a resource, but there are multiple ways in which the same final state can be achieved, and those ways actually differ in a significant but non-observable side-effect.

Implementing bulk updates within RESTful services, In a first post, we describe how to design a Web API (i.e. RESTful service). on the way to implement CRUD operations but doesnt tackle bulk updates. the request using for example a custom header. multiple element input. REST APIs enable you to develop any kind of web application having all possible CRUD (create, retrieve, update, delete) operations. REST guidelines suggest using a specific HTTP method on a particular type of call made to the server (though technically it is possible to violate this guideline, yet it is highly discouraged).

REST was NEVER about CRUD, A popular myth is that REST-based APIs must be CRUD-based – that read, update, and delete actions for some or all of the resources it offers. Using the zoom-embed pattern, the API supports more customization of the� What is Restful Web Service? Restful Web Service is a lightweight, maintainable, and scalable service that is built on the REST architecture. Restful Web Service, expose API from your application in a secure, uniform, stateless manner to the calling client. The calling client can perform predefined operations using the Restful service.

Custom REST actions (non CRUD actions) - Ember Data, I think it would also be nice to update the model with the response data from the server, What you trying to do is RPC its not REST service. Create custom APIs you can call from Azure Logic Apps. 05/26/2017; 14 minutes to read +3; In this article. Although Azure Logic Apps offers hundreds of connectors that you can use in logic app workflows, you might want to call APIs, systems, and services that aren't available as connectors.