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
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 pass the JSON as a string, then use the different Salesforce JSON parser methods to deserialize the JSON string. – read my post on Parsing JSON to know more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestResource(urlMapping=’/MyService/*’) | |
global with sharing class MyRestResource { | |
@HttpPost | |
global static void createNewCase(String jsonPayload) { | |
//simple JSON parser | |
Map o = (Map) JSON.deserializeUntyped(jsonPayload); | |
system.assertEquals(o.get(‘companyName’), ‘GenePoint’); | |
system.assertEquals(o.get(‘caseType’), ‘Software’); | |
} | |
} |
- Create the method and supply the parameters for the function. Salesforce Apex REST will attempt to deserialize the payload data into those parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestResource(urlMapping=’/MyService/*’) | |
global with sharing class MyRestResource { | |
@HttpPost | |
global static String createNewCase(String companyName, String caseType) { | |
system.assertEquals(companyName, ‘GenePoint’); | |
system.assertEquals(caseType’, ‘Software’); | |
} | |
} |
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: