duancanjiu3754 2015-07-27 12:40
浏览 44

从AppEngine的上下文中获取* http.Request

I'm using app engine, and creating context.Context (golang.org/x/net/context) variable from the *http.Request.

    c := appengine.NewContext(r)

I'm passing the context around and I'm trying to figure out a way to get the *http.Request from the context.Context in order to log the http.Request.

I search all over the doc but I couldn't find any solution.

  • 写回答

1条回答 默认 最新

  • douyue7408 2015-07-27 13:15

    appengine.NewContext(r) returns a value of type appengine.Context. This is not the same as the Context type of the golang.org/x/net/context package!

    Having a value of type appengine.Context, you can't get the *http.Request you used to create it. If you will need the *http.Request, you have to take care of passing that around yourself (you have it, since you use that to create the context).

    Note that appengine.Context (which is an interface type) has a method Context.Request(), but that is for internal use only, it is not exported for anyone to call it. And also it returns an interface{} and not a *http.Request. Even if it returns a value holding a *http.Request, you can't rely on it as this method may be changed or removed in future versions.

    Passing the *http.Request along with the appengine.Context is the best way. Trying to get it from the context is just "wizardry" and might break with a new SDK release. If you want to simplify it, you may create a wrapper struct and pass that wrapper instead of 2 values, for example:

    type Wrapper struct {
        C appengine.Context
        R *http.Request

    And a helper func:

    func CreateCtx(r *http.Request) Wrapper {
        return Wrapper{appengine.NewContext(r), r}
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥30 关于#c++#的问题,请各位专家解答!
  • ¥15 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计
  • ¥15 用联想小新14Pro
  • ¥15 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)
  • ¥15 w3wp,exe 中发生未处理的 Microsoft ,NETFramework 异常。
  • ¥20 C51单片机程序及仿真(加减器)
  • ¥15 AQWA | 水动力分析 二阶波浪力
  • ¥15 c语言题目:成绩管理系统