1

I am trying to get my request header details by inserting it as a "log__c" (which is nothing but another form of System.debug)record as seen below

@RestResource(urlMapping='/Project/*')
global class ProjectCalloutService {

   @future(callout=true)
   public static void doCallout(Id oid)
   {

    Opportunity o = [SELECT ID,Name,Account.Name,CloseDate,Amount FROM Opportunity WHERE Id = :oid];

    ServiceTokens__c s = ServiceTokens__c.getValues('ProjectServiceToken');


    String jsonInput = '{\n' +
    ' "opportunityId" : "'+o.Id+'",\n'+
    ' "opportunityName" : "'+o.Name+'",\n'+
    ' "accountName" : "'+o.Account.Name+'",\n'+
    ' "closeDate" : "'+String.ValueOf(o.CloseDate).mid(0,10)+'",\n'+   
    ' "amount" : '+o.Amount+'\n'+
    '}';

    HTTPRequest req = new HTTPRequest();
    req.setEndPoint('callout:ProjectService');
    req.setMethod('POST');
    req.setHeader('Authorization', 'Bearer ' + s.Token__c);
    req.setHeader('Content-Type', 'application/json');

    //req.setHeader('Authorization',s.Token__c);
    req.setBody(jsonInput);

    HTTP h = new HTTP();
    HTTPResponse res = h.send(req);
    //System.debug(res);

    Log__c l = new Log__c(LogText__c = String.ValueOf(res));
    insert l;
    Log__c l1 = new Log__c(LogText__c = jsoninput);
    insert l1;
    Log__c l2 = new Log__c(LogText__c = String.ValueOf(req));
    insert l2;

   }


    @InvocableMethod
    public static void postOpportunityToPMS(List<Id> oids){

     doCallout(oids[0]);

    }
}

As seen from the above code, I am setting the authorization in the header via req.setHeader('Authorization', 'Bearer ' + s.Token__c);

There is value (s.Token__c) in that custom setting record.

But for some reason when I check the "log__c" record I get the following

System.HttpRequest[Endpoint=callout:ProjectService, Method=POST]

As can be seen there is no authorization (token missing from the request) seen in the request even though I package it in my request.

Can someone tell me what I am missing ?

1
  • you actually have to get the headers
    – Eric
    Jan 30, 2017 at 5:01

2 Answers 2

5

The headers are not serializable and thus do not get translated when parsing as a string, deserializing, etc.

Instead use this:

//assuming you've already created your httprequest object
httpresponse res = http.send(req);

//get the list of header names (keys)
string[] headerkeys = res.getHeaderKeys();

//create an object to store your header key-value pairs
Map<string, string> headers = new map<string, string>();

//iterate through they keys, and populate your map
for(string s : headerkeys){
   headers.put(s,res.getHeader(s));
   system.debug('header: ' + s + ' value: ' + res.getHeader(s));
}

http://developer.force.com/cookbook/recipe/getting-all-http-headers-from-an-http-response

Now, this is assuming you want the headers of the response unlike what your code has which is the request. You already know the headers of the request as your code set them....

If you want your request headers then you can add them to something you can log later like its own map

2

When you print/Log any object the toString method is invoked internally. This method do not print sensitive information like token.

take look at this code:

HTTPRequest req = new HTTPRequest();
req.setEndPoint('callout:ProjectService');
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer XYZ' );
req.setHeader('Content-Type', 'application/json');

System.debug('>>' + req);

Output:

10:27:36:007 USER_DEBUG [7]|DEBUG|>>System.HttpRequest[Endpoint=callout:ProjectService, Method=POST]

If you explicitly want to log/print header you will have to call getHeader() method

For ex:

System.debug('>> Authorization' + req.getHeader('Authorization'));

OutPut

10:37:33:003 USER_DEBUG [9]|DEBUG|>> AuthorizationBearer XYZ

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .