linkheader

package
v0.0.0-...-883fa4f Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0, MIT Imports: 2 Imported by: 0

README

# Golang Link Header Parser

Library for parsing HTTP Link headers. Requires Go 1.6 or higher.

Docs can be found on [the GoDoc page](https://godoc.org/github.com/tomnomnom/linkheader).

[![Build Status](https://travis-ci.org/tomnomnom/linkheader.svg)](https://travis-ci.org/tomnomnom/linkheader)

## Basic Example

```go
package main

import (
	"fmt"

	"github.com/tomnomnom/linkheader"
)

func main() {
	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
```


Documentation

Overview

Package linkheader provides functions for parsing HTTP Link headers

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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

A Link is a single URL and related parameters

func (Link) HasParam

func (l Link) HasParam(key string) bool

HasParam returns if a Link has a particular parameter or not

func (Link) Param

func (l Link) Param(key string) string

Param returns the value of a parameter if it exists

func (Link) String

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

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

func ParseMultiple

func ParseMultiple(headers []string) Links

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

func (Links) FilterByRel

func (l Links) FilterByRel(r string) Links

FilterByRel filters a group of Links by the provided Rel attribute

func (Links) String

func (l Links) String() string

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

Jump to

Keyboard shortcuts

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