Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void postCalloutResponseContents() {
- Settings__c settings = [SELECT ConsumerKey__c, ClientSecret__c, Username__c, Password__c, SecurityToken__c
- FROM Settings__c
- WHERE Name = 'OurSettings'];
- String consumerKey = settings.ConsumerKey__c;
- String consumerSecret = settings.ClientSecret__c;
- String username = settings.Username__c;
- String password = settings.Password__c + settings.SecurityToken__c;
- String request = 'grant_type=password&client_id=' + consumerKey +'&client_secret=' + consumerSecret +
- '&username=' + username + '&password='+password;
- Http ourHttp = new Http();
- HttpRequest ourRequest = new HttpRequest();
- ourRequest.setBody(request);
- ourRequest.setMethod('POST');
- ourRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/oauth2/token');
- Position__c pos = [SELECT Name, Description__c FROM Position__c WHERE Name = 'Title22'];
- HttpResponse response = ourHttp.send(ourRequest);
- OAuth2 objAuthenticationInfo = (OAuth2)JSON.deserialize(response.getbody(), OAuth2.class);
- if(objAuthenticationInfo.ACCESS_TOKEN != null){
- JSONGenerator gen = JSON.createGenerator(true);
- gen.writeStartObject();
- gen.writeStringField('title', pos.Name);
- gen.writeStringField('description', pos.Job_Description__c);
- String jsonString = gen.getAsString();
- System.debug('jsonMaterials: ' + jsonString);
- Http finalHttp = new Http();
- HttpRequest finalRequest = new HttpRequest();
- finalRequest.setHeader('Authorization','Bearer '+ objAuthenticationInfo.ACCESS_TOKEN);
- finalRequest.setHeader('Content-Type','application/json');
- finalRequest.setHeader('accept','application/json');
- finalRequest.setBody(jsonString);
- finalRequest.setMethod('POST');
- finalRequest.setEndpoint(System.Label.Job_Advertisement_URL + '/services/apexrest/jobAdvertisementShow');
- HttpResponse finalResponse = finalHttp.send(finalRequest);
- System.debug('RESPONSE BODY: '+ finalResponse.getBody());
- }
- }
- public class OAuth2{
- public String ACCESS_TOKEN{get;set;}
- }
- @HttpPost
- global static Job__c postJob(String title, String description){
- Job__c thisJob = new Job__c(
- Title__c = title,
- Description__c = description
- );
- try{
- insert thisJob;
- } catch(Exception e){
- System.debug('Error: ' + e.getMessage());
- }
- return thisJob;
- }
- @HttpPost
- global static void myRESTMethod(Map<String, String> mapOfValues) {
- String value;
- for(String key : mapOfValues.keySet()) {
- value = mapOfValues.get(key);
- // all my other code
- .....
- }
- // all my other code
- ....
- }
- {
- "mapOfValues" : {
- "key1" : "Value for key1",
- "key2" : "Value for key2",
- "key3" : "Value for key3"
- }
- }
Add Comment
Please, Sign In to add comment