linkheader: github.com/tomnomnom/linkheader Index | Examples | Files

package linkheader

import "github.com/tomnomnom/linkheader"

Package linkheader provides functions for parsing HTTP Link headers

Index

Examples

Package Files

main.go

type Link struct {
    URL    string
    Rel    string
    Params map[string]string
}

A Link is a single URL and related parameters

func (Link) HasParam Uses

func (l Link) HasParam(key string) bool

HasParam returns if a Link has a particular parameter or not

func (Link) Param Uses

func (l Link) Param(key string) string

Param returns the value of a parameter if it exists

func (Link) String Uses

func (l Link) String() string

String returns the string representation of a link

type Links []Link

Links is a slice of Link structs

func Parse Uses

func Parse(raw string) Links

Parse parses a raw Link header in the form:

<url>; rel="foo", <url>; rel="bar"; wat="dis"

returning a slice of Link structs

Code:

header := "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"," +
    "<https://api.github.com/user/58276/repos?page=2>; rel=\"last\""
links := linkheader.Parse(header)

for _, link := range links {
    fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
}

Output:

URL: https://api.github.com/user/58276/repos?page=2; Rel: next
URL: https://api.github.com/user/58276/repos?page=2; Rel: last

func ParseMultiple Uses

func ParseMultiple(headers []string) Links

ParseMultiple is like Parse, but accepts a slice of headers rather than just one header string

Code:

headers := []string{
    "<https://api.github.com/user/58276/repos?page=2>; rel=\"next\"",
    "<https://api.github.com/user/58276/repos?page=2>; rel=\"last\"",
}
links := linkheader.ParseMultiple(headers)

for _, link := range links {
    fmt.Printf("URL: %s; Rel: %s\n", link.URL, link.Rel)
}

Output:

URL: https://api.github.com/user/58276/repos?page=2; Rel: next
URL: https://api.github.com/user/58276/repos?page=2; Rel: last

func (Links) FilterByRel Uses

func (l Links) FilterByRel(r string) Links

FilterByRel filters a group of Links by the provided Rel attribute

func (Links) String Uses

func (l Links) String() string

String returns the string representation of multiple Links for use in HTTP responses etc

Package linkheader imports 2 packages (graph) and is imported by 34 packages. Updated 2018-09-20. Refresh now. Tools for package owners.