http

package
v0.2.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 4 Imported by: 0

README

http

import "github.com/gechr/x/http"

Package http provides HTTP helpers: retryable status codes, status text, and Link headers.

Index

func IsRetryableStatus

func IsRetryableStatus(code int) bool

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
fmt.Println(xhttp.IsRetryableStatus(http.StatusTooManyRequests))
fmt.Println(xhttp.IsRetryableStatus(http.StatusBadGateway))
fmt.Println(xhttp.IsRetryableStatus(http.StatusNotFound))

Output:

true
true
false

func NextLink(h http.Header) string

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
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".

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

func Status(code int) string

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
fmt.Println(xhttp.Status(http.StatusNotFound))
fmt.Println(xhttp.Status(http.StatusTeapot))

Output:

404 Not Found
418 I'm a teapot

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

func IsRetryableStatus(code int) bool

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(h http.Header) string

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.

func Status

func Status(code int) string

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL