Reputation: 791
Creating a request, then writing it
var w CustomWriter
req, _ := http.NewRequest("POST", "/Foo", bytes.NewReader([]byte(<very long string>)))
req.Write(w)
func (w *CustomWriter) Write(request []byte) (int, error) {
fmt.Println(len(request))
return 0, nil
}
Output: 4096
But the body of my request is noticeably longer, but it just gets cut off when trying to write it. How do I write and send this HTTP Request that has a very long body without the body being lost or cut off?
Upvotes: 0
Views: 2707
Reputation: 1437
If you are ok with the request being turned into a huge array, this is already part of the Go stdlib:
import "net/http/httputil"
...
rData, err := httputil.DumpRequest(r, true)
...
...
func debugResponse(resp *http.Response) {
dump, _ := httputil.DumpResponse(resp, true)
fmt.Println("DEBUG:")
fmt.Printf("%q", dump)
}
Once you consume the response or request, you have to assume that it has been held in memory. Use the flag false if you don't want to print the body because it's large or binary.
Upvotes: 1
Reputation: 6844
This is not how you execute HTTP requests in Go. You need to do
req, _ := http.NewRequest("POST", "/Foo", bytes.NewReader([]byte(<very long string>)))
client := &http.Client{}
resp, err := client.Do(req)
Upvotes: 2