Standard library / net/http

http.ReadResponse()

Parse an HTTP/1.x response; close its body after reading. Expected stdout: '200 OK\n'.

Formal Syntax & Structure

go syntaxgo
func ReadResponse(r *bufio.Reader, req *Request) (*Response, error)

Parameters & Attributes

NameTypeDescription
r*bufio.ReaderSource reader.
req*RequestHTTP request associated with the operation.

Return Value

(*Response, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "bufio"  "fmt"  "io"  "net/http"  "strings")
func main() {  r := must(http.ReadResponse(bufio.NewReader(strings.NewReader("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")), nil))  defer r.Body.Close()  fmt.Println(r.StatusCode, 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)  }}
Go Reference