Guest User

Untitled

a guest
Oct 3rd, 2018
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. public static void postCalloutResponseContents() {
  2. Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
  3. FROM Settings__c
  4. WHERE Name = 'OurSettings'];
  5.  
  6. String consumerKey = settings.ConsumerKey__c;
  7. String consumerSecret = settings.ClientSecret__c;
  8. String username = settings.Username__c;
  9. String password = settings.Password__c + settings.SecurityToken__c;
  10. String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
  11. '&username=' + username + '&password='+password;
  12.  
  13. Http ourHttp = new Http();
  14. HttpRequest ourRequest = new HttpRequest();
  15. ourRequest.setBody(request);
  16. ourRequest.setMethod('POST');
  17. ourRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/oauth2/token');
  18.  
  19. Position__c pos = [SELECT Name, Description__c FROM Position__c WHERE Name = 'Title22'];
  20.  
  21. HttpResponse response = ourHttp.send(ourRequest);
  22. OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(response.getbody(), OAuth2.class);
  23.  
  24. if(objAuthenticationInfo.ACCESS_TOKEN != null){
  25.  
  26. JSONGenerator gen = JSON.createGenerator(true);
  27. gen.writeStartObject();
  28. gen.writeStringField('title', pos.Name);
  29. gen.writeStringField('description', pos.Job_Description__c);
  30. String jsonString = gen.getAsString();
  31. System.debug('jsonMaterials: ' + jsonString);
  32.  
  33. Http finalHttp = new Http();
  34. HttpRequest finalRequest = new HttpRequest();
  35.  
  36. finalRequest.setHeader('Authorization','Bearer '+ objAuthenticationInfo.ACCESS_TOKEN);
  37. finalRequest.setHeader('Content-Type','application/json');
  38. finalRequest.setHeader('accept','application/json');
  39. finalRequest.setBody(jsonString);
  40.  
  41. finalRequest.setMethod('POST');
  42. finalRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/apexrest/jobAdvertisementShow');
  43. HttpResponse finalResponse = finalHttp.send(finalRequest);
  44. System.debug('RESPONSE BODY: '+ finalResponse.getBody());
  45. }
  46. }
  47.  
  48. public class OAuth2{
  49. public String ACCESS_TOKEN{get;set;}
  50. }
  51.  
  52. @HttpPost
  53. global static Job__c postJob(String title, String description){
  54. Job__c thisJob = new Job__c(
  55. Title__c = title,
  56. Description__c = description
  57. );
  58.  
  59. try{
  60. insert thisJob;
  61. } catch(Exception e){
  62. System.debug('Error: ' + e.getMessage());
  63. }
  64. return thisJob;
  65. }
  66.  
  67. @HttpPost
  68. global static void myRESTMethod(Map<String, String> mapOfValues) {
  69.  
  70. String value;
  71. for(String key : mapOfValues.keySet()) {
  72. value = mapOfValues.get(key);
  73. // all my other code
  74. .....
  75. }
  76. // all my other code
  77. ....
  78. }
  79.  
  80. {
  81. "mapOfValues" : {
  82. "key1" : "Value for key1",
  83. "key2" : "Value for key2",
  84. "key3" : "Value for key3"
  85. }
  86. }
Add Comment
Please, Sign In to add comment