How to parametize POST request on Salesforce Apex REST

On a standard POST call to a REST endpoint. The receiving endpoint expects a payload on the body of the POST. The service then parses the content of the body.

For example on the following Salesforce endpoint

https://integration-architect-dev-ed.lightning.force.com/apexrest/MyService

I created a POST request and sent on the body the following JSON:

{"companyName":"GenePoint","caseType":"Software"}

With Apex REST here are two ways you can create your REST POST service method

  • Create the method and supply the parameters for the function. Salesforce Apex REST will attempt to deserialize the payload data into those parameters.

Note: With this method passing the following XML payload will also work.

GenePoint
Software

Consider the following when choosing which method to implement

  • If payload is easy and won’t change – method with parameters.
  • Do you have accept both JSON and XML payload – method parameters
  • For complex payload structure – method with User-defined type payload or the json String payload

Resource:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_methods.htm

Leave a Reply

Your email address will not be published. Required fields are marked *