Golang net/http.MaxBytesReader() function example

package net/http

Golang net/http.MaxBytesReader() function usage example

 package main

 import (
  "fmt"
  "io"
  "io/ioutil"
  "net/http"
 )

 func maxByte(w http.ResponseWriter, r *http.Request) {
  r.Body = http.MaxBytesReader(w, r.Body, 10)
  fmt.Println(io.Copy(ioutil.Discard, r.Body))
 }

 func main() {
  http.HandleFunc("/maxByte", maxByte)
  http.ListenAndServe(":8108", nil)
 }

NOTE : MaxBytesReader() is useful to prevent malicious attack or accidental large request.

Reference :

http://golang.org/pkg/net/http/#MaxBytesReader

Advertisement