Standard library / net/http
http.Transport.RegisterProtocol()
Register an alternate scheme before use. Use only reviewed transports for permitted destinations. Example serves a synthetic in-memory file. Expected stdout: 'hello\n'.
Formal Syntax & Structure
go syntaxgo
func (t *Transport) RegisterProtocol(scheme string, rt RoundTripper)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| scheme | string | URL scheme to register. |
| rt | RoundTripper | Concurrency-safe transport implementing the registered scheme. |
Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net/http" "testing/fstest")
func main() { tr := &http.Transport{} tr.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{"demo.txt": {Data: []byte("hello")}})) c := &http.Client{Transport: tr} r := must(c.Get("file:///demo.txt")) defer r.Body.Close() fmt.Println(string(must(io.ReadAll(r.Body))))}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}