goozzle

package module
v0.0.0-...-a9179c2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2019 License: MIT Imports: 9 Imported by: 1

README

Goozzle

PHP Guzzle flavoured HTTP client for golang

Travis Coverage Status Go Report Card GoDoc license

See also tests and examples. If you have feature requests, send issues. Enjoy.

Basic usage

package main

import (
	"log"
	"net/url"

	"github.com/alexeyco/goozzle"
)

func main() {
	u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts/1")

	res, err := goozzle.Get(u).Do()
	if err != nil {
		log.Fatal(err)
	}
	
	log.Println(res.String())
}

JSON requests

type requestStruct struct {
	Foo string `json:"foo"`
}

structValue := requestStruct{
	Foo: "bar",
}

res, err := goozzle.Post(u).JSON(&structValue)

Send form

form := url.Values{}
form.Add("Foo", "Bar")

res, err := goozzle.Post(u).Form(form)

JSON response

type responseStruct struct {
	Foo string `json:"foo"`
}

var value responseStruct

res, _ := Get(u).Do()
res.JSON(&value)

License

MIT License

Copyright (c) 2019 Alexey Popov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

Package goozzle - PHP Guzzle flavoured HTTP client for golang

Index

Examples

Constants

View Source
const (
	// Version goozzle version
	Version = 1
	// UserAgentChrome if you want to pretend to be Сhrome
	UserAgentChrome = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"
	// UserAgentSafari if you want to pretend to be Safari
	UserAgentSafari = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/604.4.7 (KHTML, like Gecko) Version/11.0.2 Safari/604.4.7"
	// UserAgentFirefox if you want to pretend to be Firefox
	UserAgentFirefox = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0"
	// UserAgentInternetExplorer if you want to pretend to be IE
	UserAgentInternetExplorer = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"
	// UserAgentEdge if you want to pretend to be Edge
	UserAgentEdge = "" /* 153-byte string literal not displayed */
)

Variables

View Source
var UserAgentDefault = fmt.Sprintf("Goozzle/%d", Version)

UserAgentDefault default goozzle user agent

Functions

This section is empty.

Types

type DebugHandler

type DebugHandler func(*Response)

DebugHandler handler function to debug requests

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request HTTP-request struct

func Delete

func Delete(url *url.URL) *Request

Delete creates DELETE request

func Get

func Get(url *url.URL) *Request

Get creates GET request

func New

func New(method string, url *url.URL) *Request

New returns request with custom method

func Post

func Post(url *url.URL) *Request

Post creates POST request

func Put

func Put(url *url.URL) *Request

Put creates PUT request

func (*Request) Body

func (r *Request) Body(body []byte) (*Response, error)

Body sets request body

func (*Request) Cookie

func (r *Request) Cookie(cookie *http.Cookie) *Request

Cookie sets request cookie

func (*Request) Debug

func (r *Request) Debug(h DebugHandler) *Request

Debug sets request debug handler func

func (*Request) Do

func (r *Request) Do() (*Response, error)

Do returns response

Example
package main

import (
	"fmt"
	"github.com/alexeyco/goozzle"
	"log"
	"net/url"
)

func main() {
	u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts/1")

	_, err := goozzle.Get(u).Debug(func(res *goozzle.Response) {
		req := res.Request()

		fmt.Println("Request")
		fmt.Println("=======")
		fmt.Println("")

		fmt.Println("URL:", req.URL().String())
		fmt.Println("")

		fmt.Println("Headers:")
		for key, val := range req.Headers() {
			fmt.Println(fmt.Sprintf("%s: %s", key, val))
		}
		fmt.Println("")

		fmt.Println("Response")
		fmt.Println("=======")
		fmt.Println("")

		fmt.Println("Status:", res.Status())
		fmt.Println("")

		fmt.Println("Headers:")
		for key, val := range res.Headers() {
			fmt.Println(fmt.Sprintf("%s: %s", key, val))
		}
		fmt.Println("")

		fmt.Println("Body:")
		fmt.Println(res.String())
		fmt.Println("")
	}).Do()

	if err != nil {
		log.Fatal(err)
	}
}
Output:

func (*Request) Form

func (r *Request) Form(v url.Values) (*Response, error)

Form sends encoded form and returns response

func (*Request) Header

func (r *Request) Header(key, value string) *Request

Header returns request header value by name

func (*Request) Headers

func (r *Request) Headers() map[string]string

Headers returns request headers map

func (*Request) JSON

func (r *Request) JSON(v interface{}) (*Response, error)

JSON sets request JSON and returns response

Example
package main

import (
	"fmt"
	"github.com/alexeyco/goozzle"
	"log"
	"net/url"
)

func main() {
	type Post struct {
		ID     int    `json:"id"`
		UserID int    `json:"userId"`
		Title  string `json:"title"`
		Body   string `json:"body"`
	}

	u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts")

	post := &Post{
		ID:     999,
		UserID: 888,
		Title:  "Some title",
		Body:   "Many hands make light work",
	}

	_, err := goozzle.Post(u).Debug(func(res *goozzle.Response) {
		req := res.Request()

		fmt.Println("Request")
		fmt.Println("=======")
		fmt.Println("")

		fmt.Println("URL:", req.URL().String())
		fmt.Println("Method:", req.Method())
		fmt.Println("")

		fmt.Println("Headers:")
		for key, val := range req.Headers() {
			fmt.Println(fmt.Sprintf("%s: %s", key, val))
		}
		fmt.Println("")

		fmt.Println("Body:")
		fmt.Println(req.String())
		fmt.Println("")

		fmt.Println("Response")
		fmt.Println("=======")
		fmt.Println("")

		fmt.Println("Status:", res.Status())
		fmt.Println("")

		fmt.Println("Headers:")
		for key, val := range res.Headers() {
			fmt.Println(fmt.Sprintf("%s: %s", key, val))
		}
		fmt.Println("")

		fmt.Println("Body:")
		fmt.Println(res.String())
		fmt.Println("")
	}).JSON(&post)

	if err != nil {
		log.Fatal(err)
	}
}
Output:

func (*Request) Method

func (r *Request) Method() string

Method returns request method

func (*Request) Referer

func (r *Request) Referer(referer string) *Request

Referer sets referer header

func (*Request) String

func (r *Request) String() string

String returns request body as string

func (*Request) URL

func (r *Request) URL() *url.URL

URL returns request URL

func (*Request) UserAgent

func (r *Request) UserAgent(userAgent string) *Request

UserAgent sets request custom user agent request header

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response HTTP-response struct

func (*Response) Body

func (r *Response) Body() []byte

Body returns response body

func (*Response) Cookie

func (r *Response) Cookie(key string) string

Cookie returns response cookie by name

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

Cookies returns response cookies slice

func (*Response) Header

func (r *Response) Header(key string) string

Header returns response header by name

func (*Response) Headers

func (r *Response) Headers() map[string]string

Headers returns response headers map

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON unmarshal JSON response to struct

Example
package main

import (
	"github.com/alexeyco/goozzle"
	"log"
	"net/url"
)

func main() {
	type Post struct {
		ID     int    `json:"id"`
		UserID int    `json:"userId"`
		Title  string `json:"title"`
		Body   string `json:"body"`
	}

	u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts/1")

	res, err := goozzle.Get(u).Do()
	if err != nil {
		log.Fatal(err)
	}

	var post Post
	err = res.JSON(&post)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(post)
}
Output:

func (*Response) Request

func (r *Response) Request() *Request

Request returns initial request

func (*Response) Status

func (r *Response) Status() int

Status returns response status code

func (*Response) String

func (r *Response) String() string

String returns response body as string

Jump to

Keyboard shortcuts

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