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
avtopi786avtopi786 

SFDC http header

Hi,

 

Looking for sample code to extract HTTP header data -- anyone done in the past ?

 

Thanks in advance !

paul-lmipaul-lmi
to process headers, you need to iterate through a string list of the header keys.  to do this, use the getHeaderKeys() method on httpresponse to grab the list of keys, and then use a for loop to to call getHeader() on each key to get a full set of values.
You should also put handling for if the for loop var ("s" in my sample) is null, because I've noticed that null header keys are pretty common in using this platform.
httpresponse res = http.send(req);

string[] headerkeys = res.getHeaderKeys();

Map<string, string> headers = new map<string, string>();

for(string s : headerkeys){
   headers.put(s,res.getHeader(s));
   system.debug('header: ' + s + ' value: ' + res.getHeader(s));
}

 

avtopi786avtopi786

Is there a way to reference request and response object from an Apex class without creating new instance. If the Apex code is running in web container then I'm assuming that you can reference request/response object using some global variable.

 

I have done lot of Apex development for integration but no exposure on Visualforce world. Let me know if I am completely off.

 

thanks !

 

 

paul-lmipaul-lmi

httprequest and httpresponse are both instance objects, not global.  I don't understand what you mean by Apex running in a web container.  Maybe you could provide more info as to what you're trying to achieve, and/or code samples of where you are?

avtopi786avtopi786

I would like to extract http header information from a request object and check for an attribute that tells me if the user has already logged into an external site.

 

Use case:

1) User logs into internal site. User session info is added to header

2) Clicks on a link that takes user to Salesforce login page

3) User logs into Salesforce

4) User clicks on a link that is outside Salesforce and requires authentication. At this point user does not require to login because user had established session on STEP 1. I want to verify that the header information still exists before redirecting user to requested site.

 

I have implemented this in Java world, not sure how it's done in Apex world though.

 

thanks!

paul-lmipaul-lmi

You need to look at using Salesforce's OAuth provider functionality.  What you're trying to do isn't possible for security reasons (browser security, not just SF).

avtopi786avtopi786

Thanks for your input. Have done some OAuth work in the past -- doesn't sound like ideal solution.