+ Start a Discussion
Nihon TaisaiNihon Taisai 

How to make callout method available for dialogue with Http-method?

Is there any way to make this code better? I need to get here msgs from HttpPost custom method: Success adding or Error msg. HttpPost method has this functions, but I don't know how to make the 'dialogue' between 2 orgs easier. Maybe I need some try-catches here too? To send messages to the other side if something is wrong.
public class Token{
    public String accessToken{get;set;}    
}

public static void postCallout() {
    //test record:
    Type__c typ = [SELECT Id, Name FROM Type__c WHERE Name = 'ttt'];
    Http ourHttp = new Http();
    //Here are my consumer key, consumer secret, username, password and request:
    String requestBody = getAccess();  
    HttpRequest req = new HttpRequest();
    req.setBody(requestBody);
    req.setMethod('POST');
    req.setEndpoint('https://p21.lightning.force.com/services/oauth2/token'); 
    HttpResponse response = ourHttp.send(req);      
    Token authentication = (Token)JSON.deserialize(response.getbody(), Token.class);

    if(objAuthenticationInfo.accessToken != null){
        HttpRequest req2 = new HttpRequest();
        req2.setHeader('Authorization','Bearer ' + authentication.accessToken);
        req2.setHeader('Content-Type','application/json');
        req2.setHeader('accept','application/json');
        req2.setMethod('POST');
        req2.setEndpoint('https://p21.lightning.force.com/services/apexrest/types/');
        req2.setBody(GenerateJSON(typ));
        response = ourHttp.send(req2);
    }
}