Hey Guys,
So this post will focus on how one can invoke any web service class( REST / SOAP ) without using any Authentication/Username & Password.
Of course, people who are working on Public domains will understand why we need to web services without any authentication.
Simple Example:
There is a Data sitting in your org (say SFDC ) which is not harmfull if exposed to any requester. ( Examples of such Public Web services are UBER, google Maps).
IMPORTANT : You must have created your own site.
Now as a demo you will visit my website built on force.com Site and fill in the form.
Once you land on the page, click Rest Example Tab.
Lets hit this.
later you will click this link appending the same Email id which you gave to create the record.
once you click the above link, paste the email id at the end of the URL in the browser.
You just hit a public Restful web-service.
You created a record in someone's anonymous site and checked how many records you actually created there.
The count is what coming from the class.
I will not talk about what you did on the form. But brief you about that.
The form is actually a Page created on Force.com Site and the detail which you entered are stored in one of the object in my salesforce instance. Thats it.
Now about Rest,
It is a simple method :
@RestResource(URLMapping='/publicRestServiceClass')
Global Class publicRestServiceClass{
@HttpGet
global static Void invokePublicRestMethod(){
String userEmail= RestContext.request.params.get('UserEmail');
List<Public_Site_Record__c> PSRList= [Select Id from Public_Site_Record__c where email__c =:userEmail ];
Integer countOfRecs = PSRList.size();
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf('You have total '+countOfRecs+' records in total with the email '+userEmail);
}
Example:
To have this kind of output you need to modify the above class.
RestContext.response.responseBody : This is a Blob Primitive data type. So what ever response you need to send back the requester, you need to send in Blob.
Once the response is retrieved, it will be requester's job to see how to use it.
If you form a Json Object and send the response, the requester will receive a Json String and using Json parser, they can break down the each element in the object.
The final output of this class will be a plain String .
So this post will focus on how one can invoke any web service class( REST / SOAP ) without using any Authentication/Username & Password.
Of course, people who are working on Public domains will understand why we need to web services without any authentication.
Simple Example:
There is a Data sitting in your org (say SFDC ) which is not harmfull if exposed to any requester. ( Examples of such Public Web services are UBER, google Maps).
IMPORTANT : You must have created your own site.
Now as a demo you will visit my website built on force.com Site and fill in the form.
Once you land on the page, click Rest Example Tab.
Lets hit this.
later you will click this link appending the same Email id which you gave to create the record.
once you click the above link, paste the email id at the end of the URL in the browser.
You just hit a public Restful web-service.
You created a record in someone's anonymous site and checked how many records you actually created there.
The count is what coming from the class.
I will not talk about what you did on the form. But brief you about that.
The form is actually a Page created on Force.com Site and the detail which you entered are stored in one of the object in my salesforce instance. Thats it.
Now about Rest,
It is a simple method :
@RestResource(URLMapping='/publicRestServiceClass')
Global Class publicRestServiceClass{
@HttpGet
global static Void invokePublicRestMethod(){
String userEmail= RestContext.request.params.get('UserEmail');
List<Public_Site_Record__c> PSRList= [Select Id from Public_Site_Record__c where email__c =:userEmail ];
Integer countOfRecs = PSRList.size();
RestContext.response.addHeader('Content-Type', 'application/json');
RestContext.response.responseBody = Blob.valueOf('You have total '+countOfRecs+' records in total with the email '+userEmail);
}
In the Above method, i am expecting only 1 parameter from you and that is UserEmail.
You can see how that is obtained in the class using RestContext.request.params.get('UserEmail');
RestContext.response.addHeader : If you don't specify the response type, the response will be in XML format
Example:
<response>
You have total 7 records in total with the email chanz707@gmail.com
</response>
To have this kind of output you need to modify the above class.
- Change the return type to String
- Create a new variable of String type (Say " String responseString ")and fill in the details what you want to send back to the requester.
- finally Return responseString;
RestContext.response.responseBody : This is a Blob Primitive data type. So what ever response you need to send back the requester, you need to send in Blob.
Once the response is retrieved, it will be requester's job to see how to use it.
If you form a Json Object and send the response, the requester will receive a Json String and using Json parser, they can break down the each element in the object.
The final output of this class will be a plain String .
" You have total 7 records in total with the email chanz****@gmail.com "
At this stage, i am assuming that you have already created a site in your org.
Now its time to create a class mentioned above.
Now you need to give the profile to access this class. Lets see how.
Setting -> Sites ->Click on the Site Label -> Click on Public Access Setting Button.
This will open up the Guest profile which requester will be accessing your web service class with.
Go to " Enabled Apex Class Access " section and give the access.
Once the setup is done, its time to check it.
Once you click on the Site Label in the above step, copy the Domain name of your site.
Copy the Domain name and append it with" /services/apexrest/YourResrURLMappingName?UserEmail=AnyParameterYouWantToPassItToYourRestClass"
That is it !! You have just accepted a complete ananymous request and processed the same and sent them back something.
Comments
Post a Comment