I am trying to implement an oauth server and the package I am using needs the complete http.ResponseWriter and http.Request types.
c.Response does not contain all the methods that http.ResponseWriter does and c.Request gives error incompatible type.
How do I get http.ResponseWriter and http.Request in a Revel controller?
type client struct {
ClientId string
ClientSecret string
}
type App struct {
*revel.Controller
}
func (c App) TokenRequest() {
r := c.Request
w := c.Response
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
log.Println(string(body))
var cli client
err = json.Unmarshal(body, &cli)
if err != nil {
panic(err)
}
log.Println(cli.ClientId)
err = OauthSrv.HandleTokenRequest(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
I am generally not fond of frameworks like Revel in Go, for reasons that I hope demonstrate themselves on this page. My first recommendation would be that you examine closely what you are actually getting out of Revel that merits the use of such a heavy abstraction layer, and if it's really that valuable, you may want to ask questions going in the other direction, such as how one might make OauthSrv
work within Revel's customized ecosystem.
For something to be an http.ResponseWriter
, it just needs to have these methods.
You need a method named Header()
that returns an http.Header
, which you can build out of any map[string][]string
. Revel provides similar functionality, but through several layers of abstraction. You will need to unravel them:
c.Response
is a *Response
, so it has a field named Out
containing an OutResponse
.OutResponse
has a Header()
method—but it doesn't return an http.Header
. Instead, it returns a *RevelHeader
.*RevelHeader
has a GetAll(key string) []string
method—which is very similar to the API already provided by the built-in map
type, but isn't exactly the same. So, you will need to copy the returned values into a new map every time Header()
is called, in order to fully satisfy the function signature requirements.GetAll()
requires you to know the key name you are interested in, and *RevelHeader
on its own does not provide a way to look up which keys are available. For now we can rely on the fact that the current implementation only has one field, a ServerHeader
that does provide a GetKeys() []string
method.Putting all this together, we can build our Header
method:
func (rrw RevelResponseWrapper) Header() http.Header {
revelHeader := rrw.Response.Out.Header()
keys := revelHeader.Server.GetKeys()
headerMap := make(map[string][]string)
for _, key := range keys {
headerMap[key] = revelHeader.GetAll(key)
}
return http.Header(headerMap)
}
You would use similar anti-patterns to expose rrw.Write([]byte) (int, error)
so that it calls through to c.Response.Out.Write(data []byte) (int, error)
, and rrw.WriteHeader(int) error
so that it calls c.Response.WriteHeader(int, string)
. Depending on what is considered appropriate for the framework, either panic on errors or fail silently, since their API doesn't expect WriteHeader
errors to be handle-able.
Unfortunately, the http.Request
type is a struct, so you can't just simulate it. You basically have two options: reconstruct it using the net/http
package from all the properties you are able to access, or hope that the *revel.Request
you have is secretly an http.Request
under the hood. In the latter case, you can use a type assertion:
revelReq, ok := c.Request.In.(*revel.GoRequest)
if !ok {
// handle this somehow
}
r := revelReq.Original