Created
April 27, 2015 11:12
-
-
Save Sunil02kumar/f39384de89f59fb8a545 to your computer and use it in GitHub Desktop.
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
public class RESTAPIPlaygroundController { | |
public String strSelectedAppId{get;set;} | |
public String strURI{get;set;} | |
public String strSelectedMethod{get;set;} | |
public String strSelectedContentType{get;set;} //JSON or XML | |
public String strRequestBody{get;set;} | |
public String strResponse{get;set;} | |
public String strApexCode{get;set;} | |
public String strAccessToken{get;set;} | |
public RESTAPIPlaygroundController(){ | |
strSelectedMethod='GET'; | |
strSelectedContentType='.xml'; | |
} | |
public list<selectoption> getAvailableHTTPmethods(){ | |
list<selectoption> options=new list<selectoption>(); | |
options.add(new selectoption('GET','GET')); | |
options.add(new selectoption('PATCH','PATCH')); | |
options.add(new selectoption('POST','POST')); | |
//options.add(new selectoption('PUT','PUT')); | |
options.add(new selectoption('DELETE','DELETE')); | |
return options; | |
} | |
public List<selectoption> getContentType(){ | |
List<selectoption> options=new List<selectoption>(); | |
options.add(new selectoption('.json','JSON')); | |
options.add(new selectoption('.xml','XML')); | |
return options; | |
} | |
public pageReference sendRequest(){ | |
String errmsg=validateInputparametrs(); | |
system.debug('*********errmsg'+errmsg); | |
if(errmsg!=null && errmsg!=''){ | |
ApexPages.addMessage(new apexpages.message(ApexPages.severity.Error,errmsg)); | |
}else{ | |
HttpRequest req=createHTTPRequest(); | |
HTTPResponse res=sendRequest(req); | |
strResponse=res+'\n'; | |
strResponse=res.getBody(); | |
} | |
return null; | |
} | |
public HttpRequest createHTTPRequest(){ | |
HttpRequest req = new HttpRequest(); | |
strApexCode='HttpRequest req = new HttpRequest();\n'; | |
String endpointURL; | |
if(strAccessToken!=null && strAccessToken!=''){ | |
//setting end point URL | |
if(strURI!=null && strURI!=''){ | |
if(strURI.endswith('/')){ | |
strURI=strURI.substring(0,strURI.length()-2); | |
} | |
endpointURL=strURI; | |
system.debug('************endpointURL:'+endpointURL); | |
if(strSelectedMethod.equalsignorecase('PATCH')){ | |
endpointURL+='?_HttpMethod=PATCH'; | |
} | |
req.setEndpoint(endpointURL); | |
strApexCode+='req.setEndpoint('+endpointURL+');\n'; | |
} | |
//setting request headers | |
if(strSelectedContentType.equalsignorecase('.xml')){ | |
req.setHeader('Content-Type', 'application/xml; charset=utf-8'); | |
strApexCode+='req.setHeader(\'Content-Type\', \'application/xml; charset=utf-8\');\n'; | |
req.setHeader('Accept', 'application/xml'); | |
strApexCode+='req.setHeader(\'Accept\', \'application/xml\');\n'; | |
}else{ | |
req.setHeader('Content-Type', 'application/json; charset=utf-8'); | |
strApexCode+='req.setHeader(\'Content-Type\', \'application/json; charset=utf-8\');\n'; | |
} | |
req.setHeader('Authorization','Authorization: Bearer '+strAccessToken); | |
strApexCode+='req.setHeader(\'Authorization\',\'Authorization: Bearer '+strAccessToken+');\n'; | |
//setting HTTP method | |
if(strSelectedMethod!=null && strSelectedMethod!=''){ | |
if(strSelectedMethod.equalsignorecase('GET')){ | |
req.setMethod('GET'); | |
strApexCode+='req.setMethod(\'GET\');\n'; | |
}else if(strSelectedMethod.equalsignorecase('POST') || strSelectedMethod.equalsignorecase('DELETE') || strSelectedMethod.equalsignorecase('PUT')){ | |
req.setMethod(strSelectedMethod); | |
strApexCode+='req.setMethod('+strSelectedMethod+');\n'; | |
string body=strRequestBody; | |
req.setBody(body); | |
strApexCode+='req.setBody('+body+');\n'; | |
req.setHeader('Content-Length', '1024'); | |
strApexCode+='req.setHeader(\'Content-Length\',\'1024\');\n'; | |
req.setTimeout(12000); | |
strApexCode+='req.setTimeout(1200);\n'; | |
} | |
else if(strSelectedMethod.equalsignorecase('PATCH')){ | |
req.setMethod('POST'); | |
strApexCode+='req.setMethod(\'POST\');\n'; | |
string body=strRequestBody; | |
req.setBody(body); | |
strApexCode+='req.setBody('+body+');\n'; | |
req.setHeader('Content-Length', '1024'); | |
strApexCode+='req.setHeader(\'Content-Length\',\'1024\');\n'; | |
req.setTimeout(12000); | |
strApexCode+='req.setTimeout(1200);\n'; | |
} | |
} | |
} | |
system.debug('***************strApexCode:'+strApexCode); | |
system.debug('***************HttpRequest req:'+req); | |
return req; | |
} | |
public HTTPResponse sendRequest(HttpRequest Req){ | |
Http http = new Http(); | |
strApexCode+='Http http = new Http();\n'; | |
HTTPResponse res = http.send(req); | |
strApexCode+='HTTPResponse res = http.send(req);\n'; | |
System.debug('****************res.getStatusCode();'+res.getStatusCode()); | |
return res; | |
} | |
public string validateInputparametrs(){ | |
String errmsg; | |
if(strAccessToken==null || strAccessToken==''){ | |
errmsg='Please specify Access Token.\n'; | |
}if(strURI==null || strURI==''){ | |
errmsg+=' Please specify REST API service URI (Endpoint URL).\n'; | |
}if(strSelectedMethod==null || strSelectedMethod==''){ | |
errmsg+=' Please specify Http method.\n'; | |
} | |
system.debug('*********errmsg'+errmsg); | |
return errmsg; | |
} | |
} |
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
<apex:page controller="RESTAPIPlaygroundController" sidebar="false"> | |
<style> | |
.table .tr .td{ | |
width:100%; | |
} | |
</style> | |
<apex:form > | |
<apex:pagemessages /> | |
<apex:pageblock mode="maindetail" title="REST API TUTORIAL"> | |
<table width="100%"> | |
<tr width="100%"> | |
<td width="50%"> | |
<table> | |
<tr><th>Request Parameters</th> </tr> | |
<tr><td><apex:outputlabel value="Enter Access token" /></td></tr> | |
<tr><td><apex:inputtextarea value="{!strAccessToken}" id="AcessToken" style="width:100%;"/> </td></tr> | |
<tr><td><apex:outputlabel value="REST API service URI" /></td></tr> | |
<tr><td><apex:inputtextarea value="{!strURI}" id="URI" style="width:100%;"/> </td></tr> | |
<tr><td><apex:outputlabel value="Choose an HTTP method to perform on the REST API service URI" /></td></tr> | |
<tr> | |
<td> | |
<apex:selectradio value="{!strSelectedMethod}" id="sm"> | |
<apex:selectoptions value="{!AvailableHTTPmethods}" /> | |
</apex:selectradio> | |
</td> | |
</tr> | |
<tr><td><apex:outputlabel value="Content Type" /></td></tr> | |
<tr> | |
<td> | |
<apex:selectlist value="{!strSelectedContentType}" size="1" id="cont"> | |
<apex:selectoptions value="{!ContentType}"/> | |
</apex:selectlist> | |
</td> | |
</tr> | |
<tr><td><apex:outputlabel value="Request Body" /></td></tr> | |
<tr><td><apex:inputtextarea value="{!strRequestBody}" id="reqbody" style="width:100%;"/> </td> </tr> | |
</table> | |
</td> | |
<td width="50%"> | |
<table> | |
<tr><th>Apex Code</th></tr> | |
<tr><td><apex:inputtextarea value="{!strApexCode}" id="ac" style="width:500px;height:300px;"/></td></tr> | |
</table> | |
</td> | |
</tr> | |
</table> | |
<br/> | |
<apex:commandbutton value="Send request" action="{!sendRequest}"/> | |
<br/> | |
<!-- Response section --> | |
<table width="100%"> | |
<tr> | |
<th>HTTP Response</th> | |
</tr> | |
<tr> | |
<td><apex:inputtextarea value="{!strResponse}" id="res" style="width:100%;height:400px;"/></td> | |
</tr> | |
</table> | |
</apex:pageblock> | |
</apex:form> | |
</apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment