Wednesday, May 15, 2024
HomeGolangUnderstanding the Limitation of Reusing File-based Request Our bodies in Go's HTTP...

Understanding the Limitation of Reusing File-based Request Our bodies in Go’s HTTP Consumer – Technical Dialogue


Whats up Go Contributors and Group,

I’m working with the Go HTTP consumer and have encountered a situation the place I must reuse a file because the physique for a number of HTTP requests. Regardless of makes an attempt to reset the file pointer utilizing the Search methodology, it seems that the file can’t be reused for subsequent requests with out reopening it. This conduct happens even when utilizing the GetBody perform within the http.Request construction to offer a contemporary copy of the request physique.

From this remark, I’ve a number of questions and am searching for insights:

Design Rationale : Is there a particular cause in Go’s HTTP consumer design that stops the reuse of file-based request our bodies, even when using strategies like Search to reset the file’s learn pointer?

File I/O and HTTP Consumer Interplay : How does the interplay between file I/O operations and the HTTP consumer’s request dealing with contribute to this limitation? Is it associated to how the usual library treats file readers in a different way from different io.Reader implementations?

Go HTML templates (information)?

Is there a cause you’re not utilizing http.ServeFile like so?

Additionally if the rub is you need them in reminiscence you may cache them your self or simply use embed:

@Dean_Davidson @Sibert

In a simplified situation i wish to ship the content material of a file to a server, and if sending file failed, sending it ought to be retried. in my case file is large enough that may’t be loaded into reminiscence, due to this fact it ought to be despatched in chunks(transfer-encoding: chunked).

The problem is that, after first attempt, request.Physique is closed by transport is closed and gained’t be re-used.

It’s worthwhile to add a GetBody perform to the Request

	// GetBody defines an non-obligatory func to return a brand new copy of
	// Physique. It's used for consumer requests when a redirect requires
	// studying the physique greater than as soon as. Use of GetBody nonetheless
	// requires setting Physique.
	//
	// For server requests, it's unused.
	GetBody func() (io.ReadCloser, error)

From: http package deal – web/http – Go Packages

@ncw

Thanks on your response. As I discussed within the query, GetBody is barely used when request physique kind are of bytes.Reader or bytes.Buffer or strings.Reader however not file.
I do perceive that when an stream is shipped out by way of golang HTTP consumer, it’s dealt with in chunks and it requires searching for to the start of the stream and meantime stream may adjustments, however i don’t see why that is conflicting with rewinding physique?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments