`HttpRequest.getPath()` does not include Function name (expected: `/my-gcp-function/helloworld/`, actual `/helloworld`)
I've got a GCP Cloud Function, called call-handler. It's got a HTTP trigger. The trigger URL is
https://**location**projectname**.cloudfunctions.net/call-handler
I need to reconstruct the original URL that triggered call-handler. Let's say the original request is
https://**location**projectname**.cloudfunctions.net/call-handler/helloworld
When I log the output of HttpRequest.getPath(), it returns /helloworld, but I need it to be /call-handler/helloworld. This is what the Javadoc says it should be.
The path part of the URI for this request, without any query. If the full URI is
{@code http://foo.com/bar/baz?this=that}, then this method will return {@code /bar/baz}.
The same goes for HttpRequest.getUri(). It returns http://**location**projectname**.cloudfunctions.net/helloworld. (Note that it returns http, which is why I need to reconstruct the original request.)
Issues
The Javadoc isn't accurate, or the code is bugged?
I've posted a work around below, but it would be really useful if this library provided helper functions to accurate generate all the information.
Workaround
A programmatic way of reconstructing the original request URL is to join these strings, in order
- value of Header
X-Forwarded-Proto(docs for headers) ://- value of Header
Host /(prefix for the cloud function name)- environment variable
K_SERVICE(docs for env-vars) HttpRequest.getPath():- IFF
getPath()has 1 or more elements: prepend with/ - join each element with
/ - Note: no trailing
/
- IFF
HttpRequest.getQuery():- IFF
getQuery()has 1 or more elements: prepend with? - join each element with
&
- IFF
In my case, step by step it would be
httpshttps://https://**location**projectname**.cloudfunctions.nethttps://**location**projectname**.cloudfunctions.net/https://**location**projectname**.cloudfunctions.net/call-handlerhttps://**location**projectname**.cloudfunctions.net/call-handler/helloworld
step by step of path construction://helloworld- ...
https://**location**projectname**.cloudfunctions.net/call-handler/helloworld(no change, no query params)- (
getQuery()is empty) - (no elements to join)
- (
It was a lot of work to figure all that out, so I thought I'd share, in case it helps someone else.