httpread

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package httpread provides common un-marshalling operations from HTTP responses.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Csv added in v1.4.18

func Csv(f func() (*http.Response, error), comma rune) ([][]string, error)

Csv reads a http response into a [][]string. The response is checked for its status code and the http.Response.Body is closed.

func JSON added in v1.4.16

func JSON(f func() (*http.Response, error), v interface{}) error

JSON reads a http response into a data container using a json decoder. The response is checked for its status code and the http.Response.Body is closed.

Example

JSON reads marshals the body of a http.Response to a struct.

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/coolduke/fritzctl/httpread"
)

func main() {
	f := func() (*http.Response, error) {
		return &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{"x": 2, "y": 5}`))}, nil
	}
	var pt = struct {
		X int
		Y int
	}{}
	httpread.JSON(f, &pt)
	fmt.Println(pt)
}
Output:

{2 5}

func String added in v1.4.16

func String(f func() (*http.Response, error)) (string, error)

String reads a http response into a string. The response is checked for its status code and the http.Response.Body is closed.

Example

String reads the body of a http.Response to a string.

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/coolduke/fritzctl/httpread"
)

func main() {
	f := func() (*http.Response, error) {
		return &http.Response{Body: ioutil.NopCloser(strings.NewReader("text"))}, nil
	}
	s, _ := httpread.String(f)
	fmt.Println(s)
}
Output:

text

func XML added in v1.4.16

func XML(f func() (*http.Response, error), v interface{}) error

XML reads a http response into a data container using an XML decoder. The response is checked for its status code and the http.Response.Body is closed.

Example

XML reads marshals the body of a http.Response to a struct.

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/coolduke/fritzctl/httpread"
)

func main() {
	f := func() (*http.Response, error) {
		return &http.Response{Body: ioutil.NopCloser(strings.NewReader(`<point><x>2</x><y>5</y></point>`))}, nil
	}
	var pt = struct {
		X int `xml:"x"`
		Y int `xml:"y"`
	}{}
	httpread.XML(f, &pt)
	fmt.Println(pt)
}
Output:

{2 5}

Types

This section is empty.

Jump to

Keyboard shortcuts

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