csession

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2015 License: MIT Imports: 2 Imported by: 0

README

csession

Csession provides a client session for http client. It has the same methods as http.Client, but it will automatically save cookies, headers and set referer. This project has part codes from github.com/chzyer/go-fetcher.

Installing csession

go get github.com/coscms/webx/lib/httpsession/csession

Quick Start

1.Before use please New or NewSession

import (
	"github.com/coscms/webx/lib/httpsession/csession"
)
session := csession.New()

or

import (
	"github.com/coscms/webx/lib/httpsession/csession"
)
session := csession.NewSession(transport, checkRedirect, jar)

NewSession's params are the same as http.Client's fields

2.If you want to customize your headers, your can set HeadersFunc as your func. Default, session.HeadersFunc = session.DefaultHeadersFunc.

session.HeadersFunc = func(req *http.Request) {
	session.DefaultHeadersFunc(req)
	req.Header.Set("Cache-Control", "max-age=0")
}

3.use session like use client

resp, err := session.Get("http://www.google.com")

forms := url.Values{
		"username": {"username"}, "password": {"password"},
	}


resp, err := session.PostForm("http://www.google.com", forms)

resp, err := session.Post("http://www.google.com")

resp, err := session.Head(...)

resp, err := sesion.Do(req)

Documents

Please visit GoWalker

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHeadersFunc

func DefaultHeadersFunc(req *http.Request)

default headers, you can use your self headers func and set to session.HeadersFunc

Types

type Session

type Session struct {
	http.Client
	HeadersFunc func(req *http.Request)
	// contains filtered or unexported fields
}

struct session has an anonymous field http.Client, so it has all public methods of Client. And it add the referer, headers, cookies to request before it to do.

func New

func New() *Session

new a default Session

func NewSession

func NewSession(transport http.RoundTripper,
	checkRedirect func(req *http.Request, via []*http.Request) error,
	jar http.CookieJar) *Session

new a custom session, the params is the same as Client's public fields

Jump to

Keyboard shortcuts

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