Loren Osborn
Loren Osborn

Reputation: 141

How do I convert a net/http.ResponseWriter to a net/http.Response

Firstly, I intentionally asked this question incorrectly in hopes that the web search engines would better understand my intended question. The more accurate question is:

If I have captured all calls to a custom net/http.ResponseWriter, how do take the values passed to it, that I have stored into a []byte buffer, how do I convert this to a net/http.Response?

I believe the answer lies in net/http.ReadResponse(), but I'm not sure how to render the Response properly for ReadResponse to understand.

Upvotes: 1

Views: 1811

Answers (1)

Bob Fanger
Bob Fanger

Reputation: 29917

The net/http/httptest package has a ResponseRecorder that can convert a ResponseWriter to a Response.

If you've captured the raw http text, http.ReadResponse() can be used, but if you already parsed things like headers, status code, you could also create the new http.Request{} struct yourself.

Look at https://golang.org/src/net/http/response.go for details.

Upvotes: 1

Related Questions