HTTP: How to read an HTTP response status code

    0

    0

    When the example code runs, it will print out the following:

    hello world 200 OK

    "fmt"
    "net/http"
    "net/http/httptest"
    
    // HTTP: How to read an HTTP response status code
    package main
    
    import (
      
    )
    
    func Example() {
      ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      w.WriteHeader(http.StatusOK)
      w.Write([]byte("hello world"))
      }))
      defer ts.Close()
    
      resp, err := http.Get(ts.URL)
      if err != nil {
      fmt.Println(err)
      }
      defer resp.Body.Close()
    
      fmt.Printf("%s\n", resp.Status)
      fmt.Printf("%d\n", resp.StatusCode)
      // Output:
      // 200 OK
      // 200
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.