Unlocking an additional layer of safety to your iPhone is less difficult than you might suppose. With Two-Factor Time-Based One-Time Password (TOTP) authentication, you may bolster your device's protection and other website safety without relying on 1/3-party apps. Here's how you could set it up:
Rest APIs enable us to perform various operations such as creating and deleting, among others. In typical situations, when the data is passed as a parameter in the REST method, it becomes straightforward to manipulate, as demonstrated in the following example.
@RestResource(urlMapping='/api/CreateContact/*')
global with sharing class RestApexAPIClass
{
@HttpPost
global static String doPost(String name,String phone ) {
Contact con = new Contact();
con.name= name;
con.phone=phone;
insert con;
return con.id;
}
}
The "doPost" method accepts a name and phone as input parameters and proceeds to assign them to the contact object, facilitating the creation of a fresh contact within the Salesforce platform.
The issue arises when data is received from an external system in the form of JSON. The question then arises: how can we obtain and parse that JSON data? Fortunately, this problem can be easily solved, as demonstrated in the following example:
@RestResource(urlMapping='/api/CreateAccount/*')
global with sharing class MyFirstRestAPIClass
{
@HttpPost
global static void doPost( ) {
RestRequest req = RestContext.request;
Blob body = req.requestBody;
String requestString = body.toString();
fromJSON ss = (fromJSON)JSON.deserialize(requestString,fromJSON.class);
system.debug(SS);
}
}
//Json Parse class
global class fromJSON{ global cls_invoiceList[] invoiceList; global class cls_invoiceList { public string totalPrice; public String statementDate; public cls_lineItems[] lineItems; public string invoiceNumber; } global class cls_lineItems { public string UnitPrice; public string Quantity; public String ProductName; } global static fromJSON parse(String json){ return (fromJSON) System.JSON.deserialize(json, fromJSON.class); }
}
The provided code is performing the following actions:
- It retrieves the current REST request using
RestContext.request
and assigns it to thereq
variable. - It retrieves the request body as a
Blob
usingreq.requestBody
and assigns it to thebody
variable. - It converts the
Blob
into a string representation usingbody.toString()
and assigns it to therequestString
variable.
- It uses the
JSON.deserialize
method to convert therequestString
into an instance of thefromJSON
class. ThefromJSON
class is expected to represent the structure of the JSON data.
- It assigns the deserialized object to the variable
ss
.
- Finally, it logs the value of
ss
usingSystem.debug()
.
In summary, the code is extracting the JSON data from the request body, deserializing it into an object of a specific class (
fromJSON
), and then printing the resulting object for debugging purposes.