On Tue, Feb 5, 2019 at 9:12 AM <matteo.biage...@gmail.com> wrote: > > I've the following situation: > I proxy a request to another server and when I made a POST and create a new > request, the contentLength is zero: > > req2, _ := http.NewRequest(req.Method, newApiUrl , req.Body) > fmt.Println("New request from body:", req2.ContentLength) // print 0 > > Checking in the source code of the NewRequest func Body don't respect some > interface and populate the ContentLength field.
When the first request is created, req.Body is set to a NopCloser, and that doesn't match any of the cases in the second NewRequest. For this to work, I guess the best way is to get the body length and set it explicitly after constructing the second request. I don't know if this is intentional or not, but in general, it might be a good idea to add a ReaderWrapper interface to the standard library that defines a GetNestedReader() io.Reader function to access the real reader from these utility classes. > > Could be a bug? Which could be a valid approach in order to create a new > request from an existing one and correct set the Body length? > > A working example here: > > https://play.golang.org/p/SvCDLj0NrXb > > Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.