You need to sign in to do that
Don't have an account?
Jyoti Navale
When to use RestRequest/RestResponse and when to use HttpResuest/HttpResponse?
When to use RestRequest/RestResponse and when to use HttpResuest/HttpResponse?
I am learning REST in Saleforce. I know there are methods like GET, POST, PUT, PATCH, DELETE.
I have written a code for this (actually taken an example from a help page from Salesforce)
I want to know how to call these methods?
I suppose I can call them from cURL....if so how?
Currently I am trying to call them from Anonymous window of developer Console like this:
And I am getting error like this :
"
Response : [{"errorCode":"UNSUPPORTED_MEDIA_TYPE","message":"Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded"}]
"
Now my concern is....when can I use RestRequest/RestResponse and when to use HttpResuest/HttpResponse?...Any help?
I am learning REST in Saleforce. I know there are methods like GET, POST, PUT, PATCH, DELETE.
I have written a code for this (actually taken an example from a help page from Salesforce)
@RestResource(urlMapping='/FieldCases/*') global with sharing class myRESTCaseController { // create new case : POST method to create @HttpPost global static String createNewCase(String companyName, String caseType) { System.debug('COMPANY: '+companyName); System.debug('CASE TYPE: '+caseType); List<Account> company = [Select ID, Name, Email__c, BillingState from Account where Name = :companyName]; List<Support_Member__c> support; if(company.size() > 0) { support = [SELECT ID, User__c, Name, User__r.Email from Support_Member__c WHERE Support_State__c = :company[0].BillingState LIMIT 1]; } List<Support_Documentation__c> doc = [SELECT ID from Support_Documentation__c WHERE Type__c = :caseType ORDER BY CreatedDate ASC LIMIT 1]; if(company.size() == 0 || support.size() == 0 || doc.size() == 0) { return 'No support data exists for this problem'; } Case c = new Case(); c.OwnerId = support[0].User__c; c.AccountId = company[0].Id; c.Subject = caseType + ' for '+companyName; c.Status = 'Open'; insert c; //sendEmail(companyName, company[0].Email__c, doc[0].Id, support[0].Name, support[0].User__r.Email); return 'Submitted case to '+support[0].Name+' for '+companyName+'. Confirmation sent to '+company[0].Email__c; } // delete an old case : DELETE method used @HttpDelete global static String deleteOldCases() { String companyName = RestContext.request.params.get('companyName'); Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName]; List<Case> cases = [SELECT Id, Subject, Status, OwnerId, Owner.Name from Case WHERE AccountId =: company.Id AND Status = 'Closed']; delete cases; return 'Closed Cases Deleted'; } // update a case : PATCH method used for updation @HttpPatch global static String updateCase(String caseId, String caseStatus, String caseNote) { Case companyCase = [SELECT Id, Subject, Status, Description from Case WHERE Id =: caseId]; companyCase.Status = caseStatus; companyCase.Description += '/n/n'; companyCase.Description += caseNote; update companyCase; return 'Case Updated'; } // updates a case : PUT method used to update/insert @HttpPut global static String updateCase() { RestRequest req; String companyName = req.params.get('companyName'); Account company = [ Select ID, Name, Type, BillingState from Account where Name = :companyName]; Attachment a = new Attachment(); a.ParentId = company.Id; a.Name = 'test.png'; a.Body = req.requestBody; insert a; return 'Attachment added'; } // get Open case : GET method to get records @HttpGet global static List<Case> getOpenCases() { String companyName = RestContext.request.params.get('companyName'); Account company = [ Select ID, Name, Email__c, BillingState from Account where Name = :companyName]; List<Case> cases = [SELECT Id, Subject, Status, OwnerId, Owner.Name from Case WHERE AccountId =: company.Id]; return cases; } }
I want to know how to call these methods?
I suppose I can call them from cURL....if so how?
Currently I am trying to call them from Anonymous window of developer Console like this:
String companyName = 'Edge Communications'; String caseType = 'Electrical'; Http http = new Http(); //HttpRequest request = new HttpRequest(); // http format RestRequest request = RestContext.Request; // rest format*** request.setEndpoint('https://instance.salesforce.com/services/apexrest/FieldCase?'); // calling method request.setMethod('POST'); // pass parameteres : POST method request.setBody('companyName='+EncodingUtil.urlEncode(companyName, 'UTF-8')+'&caseType='+EncodingUtil.urlEncode(caseType, 'UTF-8')); // header params request.setHeader('Authorization', 'Bearer '+userinfo.getSessionId()); request.setCompressed(true); // response //HttpResponse response = http.send(request); RestResponse response = RestContext.response;*** System.debug('Request : '+request.toString()); System.debug('Response : '+response.getBody());
And I am getting error like this :
"
Response : [{"errorCode":"UNSUPPORTED_MEDIA_TYPE","message":"Content-Type header specified in HTTP request is not supported: application/x-www-form-urlencoded"}]
"
Now my concern is....when can I use RestRequest/RestResponse and when to use HttpResuest/HttpResponse?...Any help?
RestRequest/Response can be used to expose your Apex methods as Rest service. Now external application can access apex rest services by using HttpRequest/HttpResponse (Http Callouts).
You can write HttpRequest/HttpResponse in apex to call external applications by specifying their End Point URL.
add Adanced Rest Client to Chrome browser. It will helfs you to test Rest applications.
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo
Ex:
1. Calling external Rest services from Salesforce :
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpointURL('https://www.sampleurl');
req.setHeader('set properties');
2.Exposing Apex methods to rest service :(now external applications can access salesforce services by using HttpRequest/HttpResponse)
Regards,
Surya
Reference: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_restcontext.htm
2) HttpRequest/HttpResponse are like outbound callers which is opposite to point #1. This callout from Salesforce environment to external system and external system handles that request and provide resposne to Salesforce environmen.
Reference: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm
Solution:
Put following line also with other request headers:
or