function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

Where to put authorization in HTTP Request

Hello, I am new to Integrations/ SOAP API, so apologize if this is an elementary question. I received documentation on how to fill the body of the http request, but I realize that I also need to add authorization into the request. I have the key and secret, but I don't know where to put that in the code. 
1. Where would I put it in the request?
2. What would the key/value pair look like in the JSON? 
Here is what my request looks like so far:

       public Object  makePostCallout() {
    Lead ref =[Select Id, firstname, lastname, gender__c, patient_dob__c, patient_ssn__c from Lead where Id = :ApexPages.currentPage().getParameters().get('id')]; 
           system.debug('***NAME OF REFERRAL***: '+ ref.firstname + ' ' + ref.lastname);
    String ssnReplace; 
           if (ref.Patient_SSN__c != null){
            ssnReplace = ref.Patient_SSN__c.replace('-','');
           }
    String genderLetter;
           if (ref.Gender__c == 'Male'){
               genderLetter = 'M';
           } else if (ref.Gender__c == 'Female'){
               genderLetter = 'F';
           }
           
           String first;
           String second;
           String third;
            String d = String.valueOf(ref.Patient_DOB__c);
           system.debug('***dob string raw: ' + d);
                first = d.substring(0,4);
                second = d.substring(5,7);
                third = d.substring(8,10);
           String dob = first + second + third;
           system.debug('***dob formatted: ' + dob);
           
           Map<string, string> patientInfoMap = new Map<string, string>();
                          patientInfoMap.put('social_security_number', ssnReplace);
                          patientInfoMap.put('birthdate', dob);
                          patientInfoMap.put('gender', genderLetter);
                          patientInfoMap.put('last_name', ref.lastname);
                          patientInfoMap.put('first_name', ref.firstname);
           List<Object> patientInfoList = new List<Object>();
           patientInfoList.add(patientInfoMap);
           
           JSONGenerator gen = JSON.createGenerator(true);   
            gen.writeStartObject();     
             gen.writeStringField('processing_mode', 'Realtime');
             gen.writeStringField('request_type', '270');
             gen.writeStringField('information_source_id', '1');
             gen.writeStringField('submitter_system_id', '2');
             gen.writeStringField('submitter_system_type', 'api');
             gen.writeStringField('submitter_request_id', 'R1');
                 gen.writeStringField('submitter_user_id', '99');
            gen.writeObjectField('subscribers', patientInfoList);
              gen.writeEndObject();   
         String jsonS = gen.getAsString();
        System.debug('***jsonMaterials***'+jsonS);
          
       // Continuation con = new Continuation(40);
        //con.continuationMethod='processResponse';

       HttpRequest req = new HttpRequest();
        req.setEndpoint('https://...(left out)');
        req.setMethod('POST');
        req.setBody(jsonS);
       Http http = new Http();  
       //  this.requestLabel = con.addHttpRequest(req);
     HTTPResponse res = http.send(req);
      // return con; 
           
  String result = null;
    Integer statusCode = res.getStatusCode();
          system.debug('***statusCode is***: ' + statusCode);
   if(statusCode == 200){
             system.debug('***API invoked successfully***');
    result = res.getBody();
          system.debug('***RESULT***: ' + result);
      }   
           return result;   
        
    }
 
Vinodraj ShivanandaVinodraj Shivananda
Are you working on SOAP or REST API Integration? The code above looks like you are hitting the RESP API endpoint
Michael MMichael M
Sorry I am using the REST API. I mean to say REST, thank you
Vinodraj ShivanandaVinodraj Shivananda
You can use Named Credentials. It lets you store the credentials to login to the external system. Please let me know if you still face issues. 
Michael MMichael M
Hi Vinodraj, 

I tried adding a Named Credential, but I do not see where to type in the key and secret from my integration documentation. I added a New Named Credential, and I had to create a Certificate to do so, so I created a Self-Signed Certificate, but nowhere did I see where I can type in the key and secret from the integration docs?