Getting * http.Request from context on AppEngine
I am using application engine and create a variable context.Context
(golang.org/x/net/context) from *http.Request
.
c := appengine.NewContext(r)
I am passing in a context and I am trying to find a way to get *http.Request
from context.Context
to register http.Request
.
I search the whole document but couldn't find any solution.
source to share
appengine.NewContext(r)
returns a value of type appengine.Context
. This is not the same as the package type !Context
golang.org/x/net/context
With a type value appengine.Context
, you cannot get *http.Request
what you used to create it. If you need to *http.Request
, you have to take care to pass this around you (you have this, since you use this to create context).
Note that appengine.Context
(which is an interface type) has a method Context.Request()
, but this is for internal use only, it is not exported for anyone to call it. And also returns interface{}
instead of *http.Request
. Even if it returns a containing value, *http.Request
you cannot rely on it as this method may be changed or removed in future versions.
Passing *http.Request
along with appengine.Context
is the best way. Trying to get it out of context is just "magic" and might break with a newer version of the SDK. If you want to simplify it, you can create a wrapper structure and pass that wrapper instead of two values, for example:
type Wrapper struct {
C appengine.Context
R *http.Request
}
And the helper function:
func CreateCtx(r *http.Request) Wrapper {
return Wrapper{appengine.NewContext(r), r}
}
source to share