Skip to main content

Stream Request Body

Github StackBlitz

This project demonstrates how to handle streaming request bodies in a Go application using the Fiber framework.

Prerequisites

Ensure you have the following installed:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/stream-request-body
  2. Install dependencies:

    go get

Running the Application

  1. Start the application:
    go run main.go

Example

Here is an example of how to handle a streaming request body in Go using Fiber:

package main

import (
"github.com/gofiber/fiber/v2"
"io"
"log"
)

func main() {
app := fiber.New()

app.Post("/upload", func(c *fiber.Ctx) error {
// Open a file to write the streamed data
file, err := os.Create("uploaded_file")
if err != nil {
return err
}
defer file.Close()

// Stream the request body to the file
_, err = io.Copy(file, c.BodyStream())
if err != nil {
return err
}

return c.SendString("File uploaded successfully")
})

log.Fatal(app.Listen(":3000"))
}

References