Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Getting all HTTP headers from an HTTP Response

Getting all HTTP headers from an HTTP Response

Post by paul-lmi  (2010-10-15)

Status: Unverified
Level: intermediate


You want to retrieve custom HTTP headers from an HTTPResponse, but you may not know what headers exists. Since Apex assumes you know what headers are in play, you need a way to get all headers, even the ones you don't know about.


Use httpresponse.getHeaderKeys(), and loop through them all to get a list of what's available.

//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){
   system.debug('header: ' + s + ' value: ' + res.getHeader(s));


This recipe uses the getHeaderKeys() method to retrieve the keys - the names of the headers. It then uses getHeaders() to retrieve the values associated with those headers.


Recipe Activity - Please Log in to write a comment

Be the first to comment.


Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.