Friday, May 17, 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 Shopper – Technical Dialogue


Howdy Go Contributors and Group,

I’m working with the Go HTTP shopper and have encountered a state of affairs the place I have to 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 technique, it seems that the file can’t be reused for subsequent requests with out reopening it. This habits happens even when utilizing the GetBody perform within the http.Request construction to offer a contemporary copy of the request physique.

From this statement, I’ve a number of questions and am looking for insights:

Design Rationale : Is there a selected motive in Go’s HTTP shopper design that forestalls 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 Shopper Interplay : How does the interplay between file I/O operations and the HTTP shopper’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 motive you’re not utilizing http.ServeFile like so?

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

@Dean_Davidson @Sibert

In a simplified state of affairs i wish to ship the content material of a file to a server, and if sending file failed, sending it must be retried. in my case file is large enough that may’t be loaded into reminiscence, subsequently it must 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 is advisable to add a GetBody perform to the Request

	// GetBody defines an elective func to return a brand new copy of
	// Physique. It's used for shopper 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 – internet/http – Go Packages

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments