top of page
  • Writer's picturePaul Nguyen

Accessing HTTP Headers in Google Cloud Functions with Java

Updated: Jan 27, 2023

If you're writing a Google Cloud Function that is an HTTP Function responding to HTTP(S) requests, you may want to access the headers from the request in your Function as part of any processing you do.


If you look at the Google Cloud Function documentation on it, it's really limited on information to access the headers so I wanted to capture it for anyone else who's trying to set it up for the first time when writing a Function in Java.


Create your HTTP Function

Google does provide good documentation on a sample HTTP Function you can write, deploy and test by following their tutorial. After you've done these basics, then you can add your code to access the HTTP headers as described below.


Access the HTTP Headers

In your service() function, you first access the headers by calling getHeaders() from the HttpRequest object parameter of service():

@Override
public void service(HttpRequest request, HttpResponse response) 
  throws Exception {
    BufferedWriter writer = response.getWriter();

    Map<String, List<String>> headers = request.getHeaders();
    
}    

This map will return all the headers where each key in the map is the header name and the list has all values for that particular header. Per the official documentation, since a header name can appear multiple times in the headers, the list is returned to handle scenarios such as:

Connection: value Someheader: some value Someheader: another value

In which case headers.get("Connection") would return a List<String> with one element while headers.get("Someheader") would return a List<String> with two elements:

@Override
public void service(HttpRequest request, HttpResponse response) 
  throws Exception {
    BufferedWriter writer = response.getWriter();

    Map<String, List<String>> headers = request.getHeaders();
    
    List<String> cHeaderValues = headers.get("Connection");
    //cHeaderValues.size() = 1
    String cHeaderValue = cHeaderValues.get(0); // "value"
    
    List<String> sHeaderValues = headers.get("Someheader");
    //sHeaderValues.size() = 2
    String sHeaderValue1 = sHeaderValues.get(0); // "some value"
    String sHeaderValue2 = sHeaderValues.get(1); // "another value"
}    
Note if you have custom headers and you name them camel case, you will only be able to get the header if only the first letter is capitalized.

I originally named a header in my request to my Google Cloud Function as ContentId but it was always returning a list of 0 elements when I used the above code to call headers.get("ContentId"). From parsing the list of headers, I noticed that it was showing as "Contentid" even though I was setting it as "ContentId" in my application making the request (I also did this in Postman and saw the same thing).


I've reached out to GCP to see if this is intended and they may have changed this to honor the correct camel casing after this writing but as a heads up if you run into the same issue.


Feel free to reach out to me if you want to discuss this or anything else further.

 

Saiborne offers software consulting services to help companies meet their business objectives and get the most value out of their software products, notably the entire software lifecycle from design to development to support and implementation. Contact us to find out how we can help you with any software needs.


44 views
bottom of page