Multipart Streaming Example
This example demonstrates streaming data between two servers using http-client.
Architecture
- Server 1 (:8080): Generates a large file and returns it upon request to
/file.
- Server 2 (:8081): Accepts a multipart request on
/upload and save file.
- Client: Requests the file from Server 1, streams it directly into a multipart request to Server 2.
Running
Run the servers and client in separate terminals:
- Server 1:
cd server1
go run main.go
- Server 2:
cd server2
go run main.go
- Client (after starting the servers):
go run main.go
Alternative: Without Fluent API
For comparison, there's an alternative implementation in multipart_straming_without_fluent_api/main.go that achieves the same streaming behavior using only standard Go libraries (net/http, mime/multipart, io.Pipe) without the custom http-client library or fluent API.
To run the alternative client:
cd multipart_straming_without_fluent_api
go run main.go
This version demonstrates how to implement multipart streaming manually, providing the same memory-efficient results.
Result
- Server 1 generates file with numbered lines.
- Client streams the file from Server 1 to Server 2 without intermediate storage.
- Server 2 saves the file in its directory.
- Client outputs upload confirmation.
- Streaming progress in the logs
Notes
- Data is transferred in a streaming manner, without loading into the client's memory.
- Uses
io.Reader for streaming.
- In a real application, replace file generation with reading from a source.