Documentation
¶
Overview ¶
Package http provides HTTP helpers: retryable status codes, status text, and Link headers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsRetryableStatus ¶
IsRetryableStatus reports whether an HTTP status code represents a transient failure worth retrying: a request timeout (408), rate limiting (429), or any server error (5xx).
Example ¶
package main
import (
"fmt"
"net/http"
xhttp "github.com/gechr/x/http"
)
func main() {
fmt.Println(xhttp.IsRetryableStatus(http.StatusTooManyRequests))
fmt.Println(xhttp.IsRetryableStatus(http.StatusBadGateway))
fmt.Println(xhttp.IsRetryableStatus(http.StatusNotFound))
}
Output: true true false
func NextLink ¶
NextLink returns the rel="next" target from an RFC 8288 Link header, or "" when none. The target is returned as written - possibly relative - so a caller that needs an absolute URL resolves it against the request URL. All Link header lines are searched, an unquoted rel token is tolerated, and a quoted rel list (e.g. rel="next last") matches on any member.
Example ¶
package main
import (
"fmt"
"net/http"
xhttp "github.com/gechr/x/http"
)
func main() {
h := http.Header{}
h.Add(
"Link",
`<https://api.github.com/repos/o/r/tags?page=2>; rel="next", <https://api.github.com/repos/o/r/tags?page=5>; rel="last"`,
)
fmt.Println(xhttp.NextLink(h))
}
Output: https://api.github.com/repos/o/r/tags?page=2
Example (RelList) ¶
A quoted rel list matches on any member, and the empty string is returned when no link carries rel="next".
package main
import (
"fmt"
"net/http"
xhttp "github.com/gechr/x/http"
)
func main() {
h := http.Header{}
h.Add("Link", `<https://example.com/?page=3>; rel="next last"`)
fmt.Println(xhttp.NextLink(h))
fmt.Println(xhttp.NextLink(http.Header{}) == "")
}
Output: https://example.com/?page=3 true
func Status ¶
Status returns a human-readable form of an HTTP status code, pairing the numeric code with its canonical reason phrase, e.g. "404 Not Found".
Example ¶
package main
import (
"fmt"
"net/http"
xhttp "github.com/gechr/x/http"
)
func main() {
fmt.Println(xhttp.Status(http.StatusNotFound))
fmt.Println(xhttp.Status(http.StatusTeapot))
}
Output: 404 Not Found 418 I'm a teapot
Types ¶
This section is empty.