Standard library / net/http

http.Request.Write()

Serialize an outgoing HTTP/1.1 request to a writer. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Request) Write(w io.Writer) error

Parameters & Attributes

NameTypeDescription
wio.WriterDestination writer.

Return Value

error

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "net/http"  "strings")
func main() {  r := must(http.NewRequest("GET", "https://example.invalid/", nil))  var b bytes.Buffer  check(r.Write(&b))  fmt.Println(strings.HasPrefix(b.String(), "GET / HTTP/1.1\r\n"))}
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)  }}
Go Reference