GET, POST, PUT, PATCH and DELETE methods in Pega Integration

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

MethodPurposeExampleTypical success code
GETRead a resource. Safe and repeatableGET /customers/42200
POSTCreate a new resource. Not repeatable, a second call creates a second recordPOST /customers201
PUTReplace a resource completely. RepeatablePUT /customers/42200 or 204
PATCHChange part of a resourcePATCH /customers/42200 or 204
DELETERemove a resourceDELETE /customers/42204

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/42 returns the customer as JSON.
  • POST /customers with a JSON body creates a customer and returns 201 with the new ID.
  • PUT /customers/42 replaces the whole customer with the body.
  • PATCH /customers/42 with {"phone":"555-0100"} changes only the phone number.
  • DELETE /customers/42 closes 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