Every REST call carries a verb that says what you want to do with a resource. Getting the verbs right matters twice in Pega: when you call another system with Connect-REST, and when you publish your own API with Service REST.
The five methods
| Method | Purpose | Example | Typical success code |
|---|---|---|---|
| GET | Read a resource. Safe and repeatable | GET /customers/42 | 200 |
| POST | Create a new resource. Not repeatable, a second call creates a second record | POST /customers | 201 |
| PUT | Replace a resource completely. Repeatable | PUT /customers/42 | 200 or 204 |
| PATCH | Change part of a resource | PATCH /customers/42 | 200 or 204 |
| DELETE | Remove a resource | DELETE /customers/42 | 204 |
In Pega: calling a service (Connect-REST)
On the Connect-REST rule you choose the HTTP method and the URL, and you map the request body for POST, PUT and PATCH from your clipboard properties. For GET and DELETE there is usually no body, only the URL and its parameters. The response tab maps the reply back onto the clipboard.
In Pega: publishing a service (Service REST)
A Service REST rule defines a resource path, and for each HTTP method you attach an activity or data page that does the work. So GET /customers/{id} can run a data page that returns one customer, while POST /customers runs an activity that creates one.
A worked example
A bank publishes a customer API.
GET /customers/42returns the customer as JSON.POST /customerswith a JSON body creates a customer and returns 201 with the new ID.PUT /customers/42replaces the whole customer with the body.PATCH /customers/42with{"phone":"555-0100"}changes only the phone number.DELETE /customers/42closes the customer and returns 204.
Common mistakes
- Using POST for everything. It hides the intent and stops clients and caches from working properly.
- Using PUT when you only mean to change one field, which can wipe out the others.
- Changing data in a GET request. GET must never have side effects.
- Returning 200 for every result. Use the right status code so callers can react.
Interview tip
List the five verbs with one example each, then note which are safe (GET) and which are repeatable (GET, PUT, DELETE). Related: Service REST in Pega and exception handling in Connect-REST.
No comments:
Post a Comment