httpext

package module
v0.0.0-...-1341d77 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

README

Go Extended Http-Client

GoDoc Go Report Card

Installation:

Tor have to run on your machine if you want to send Request over the onion network.

how to install tor-service

go get github.com/lostbrokenkey/httpext

Instantiation
client := httpext.New()
Onion-Network
// socks5://127.0.0.1:9050 is default
client.enableTor()

// in other cases simply set:
proxy := Proxy{"127.0.0.1", 9050}
client.EnableTorProxy(proxy)

User-Agent
// default user-agent
// regular:     Go-httpext-client/1.0
// tor enabled: a random user-agent is generated

// to set a custom user-agent:
client.Header.UserAgent = "my custom user-agent"

Examples:

GET Html
package main

import "github.com/lostbrokenkey/httpext"

func main() {

	client := httpext.New()
	client.EnableTor()
	res, _ := client.Get("http://example.com/")
	doc, _ := res.HTML()
	node := doc.Find("h1")
	println(node.Text())

}

more information about goquery: https://github.com/PuerkitoBio/goquery

GET JSON
package main

import "github.com/lostbrokenkey/httpext"

type r struct {
	IP string `json:"ip"`
}

func main() {

	client := httpext.New()
	client.EnableTor()
	res, _ := client.Get("https://api.ipify.org?format=json")
	var response r
	_ = res.JSON(&response)
	println(response.IP)

}
GET Feed
package main

import (
	"fmt"

	"github.com/lostbrokenkey/httpext"
)

func main() {

	client := httpext.New()
	url := "https://www.nasa.gov/rss/dyn/breaking_news.rss"
	res, err := client.Get(url)
	feed, _ := res.Feed()
	if err == nil {
		fmt.Printf("%s", feed.Description)
	}

}
POST Json
package main

import (
	"fmt"

	"github.com/lostbrokenkey/httpext"
)

type Test struct {
	Field string `json:"the-field"`
}

func main() {

	t := Test{Field: "hello world"}

	client := httpext.New()
	url := "https://webhook.site/xxxxxxxx-xxxxxxxxx-xxxxxxxxx"
	res, _ := client.PostJSON(url, t)
	str, _ := res.String()
	fmt.Println(str)

}

Dependencies

have fun 😉

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Header Header
	// contains filtered or unexported fields
}

Client ...

func New

func New() Client

New creates a new instance of the tor client

func (*Client) EnableTor

func (c *Client) EnableTor()

EnableTor with default proxy

func (*Client) EnableTorProxy

func (c *Client) EnableTorProxy(proxy *Proxy)

EnableTorProxy with custom proxy

func (Client) Get

func (c Client) Get(url string) (*HTTPResponse, error)

Get requests url and return http.Response

func (Client) Post

func (c Client) Post(url string, data string) (*HTTPResponse, error)

Post ...

func (Client) PostJSON

func (c Client) PostJSON(url string, v interface{}) (*HTTPResponse, error)

PostJSON posts object as JSON

func (Client) PostMsgPack

func (c Client) PostMsgPack(url string, v interface{}) (*HTTPResponse, error)

PostMsgPack posts object as XML

func (Client) PostXML

func (c Client) PostXML(url string, v interface{}) (*HTTPResponse, error)

PostXML posts object as XML

func (Client) Request

func (c Client) Request(httpMethod string, reqURL string, body *string) (*HTTPResponse, error)

Request ...

func (Client) SendJSON

func (c Client) SendJSON(httpMethod string, url string, v interface{}) (*HTTPResponse, error)

SendJSON sends object as JSON

func (Client) SendMsgPack

func (c Client) SendMsgPack(httpMethod string, url string, v interface{}) (*HTTPResponse, error)

SendMsgPack sends object as MsgPack

func (Client) SendXML

func (c Client) SendXML(httpMethod string, url string, v interface{}) (*HTTPResponse, error)

SendXML sends object as XML

type HTTPResponse

type HTTPResponse struct {
	http.Response
}

HTTPResponse ...

func (HTTPResponse) Feed

func (r HTTPResponse) Feed() (*gofeed.Feed, error)

Feed the response as *gofeed.Feed

func (HTTPResponse) FromMsgPack

func (r HTTPResponse) FromMsgPack(v interface{}) error

FromMsgPack the response as *gofeed.Feed

func (HTTPResponse) HTML

func (r HTTPResponse) HTML() (*goquery.Document, error)

HTML the response as *goquery.Document

func (HTTPResponse) JSON

func (r HTTPResponse) JSON(v interface{}) error

JSON requests url and writes data to interface v

func (HTTPResponse) String

func (r HTTPResponse) String() (*string, error)
type Header struct {
	Timeout       time.Duration
	UserAgent     string
	CustomHeaders map[string]string
}

Header of the http-request

func NewHeader

func NewHeader() Header

NewHeader returns a new Header instance

func (Header) Prepare

func (h Header) Prepare(header *http.Header)

Prepare the http header

type Proxy

type Proxy struct {
	IP   string
	Port uint16
}

Proxy for custom proxy settings

func (Proxy) String

func (p Proxy) String() string

Jump to

Keyboard shortcuts

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