gohttp

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

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

Go to latest
Published: Feb 7, 2018 License: MIT Imports: 10 Imported by: 0

README

gohttp

Build Status Go Report Card Coverage Status

HTTP client for Go, its also support asynchronous request

Installation

go get github.com/nahid/gohttp
Example
POST https://httpbin.org/post
package main

import (
	"github.com/nahid/gohttp"
	"fmt"
)

func main() {
	req := gohttp.NewRequest()

	resp, err := req.
		FormData(map[string]string{"name": "Nahid"}).
		Post("https://httpbin.org/post")

	if err != nil {
		panic(err)
	}

	if resp.GetStatusCode() == 200 {
		var resps map[string]interface{}

		_ = resp.GetBodyWithUnmarshal(&resps)
		fmt.Println(resps["form"])
	}
}
Async Example
package main

import (
	"github.com/nahid/gohttp"
	"fmt"
)

func main() {
	req := gohttp.NewRequest()
	ch := make(chan *gohttp.Response)

	var users [3]string

	users[0] = "nahid"
	users[1] = "shipu"
	users[2] = "sujan"

	for i:=0; i<len(users); i++ {
		req.
		FormData(map[string]string{"user": users[i]}).
		AsyncPost("http://domain.app/send", ch)
	}


	for i:=0; i<len(users); i++ {
		op := <-ch

		fmt.Println(op.GetBodyAsString())
	}
}
Available Method
  • NewRequest(options ...Option)
Request
  • Get(url string)
  • Post(url string)
  • Put(url string)
  • Patch(url string)
  • Delete(url string)
Async Request
  • AsyncGet(url string, ch chan)
  • AsyncPost(url string, ch chan)
  • AsyncPut(url string, ch chan)
  • AsyncPatch(url string, ch chan)
  • AsyncDelete(url string, ch chan)
Data Bindings
  • Headers(data map[string]string)
  • FormData(data map[string]string)
  • Json(data map[string]interface{})
  • Query(data map[string]string{})
  • Body(body []byte)
  • Text(text string)
  • BasicAuth(username, password string)
  • MultipartFormData(data map[string]string{})
  • Upload(name, file string)
  • Uploads(files map[string]string{})
Response
  • GetResp()
  • GetStatusCode()
  • GetBody()
  • GetBodyAsByte()
  • GetBodyAsString()
  • GetBodyWithUnmarshal(v interface{})

See API doc https://godoc.org/github.com/nahid/gohttp

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option is an interface of request option

type OptionFunc

type OptionFunc func(*Request)

OptionFunc is an implementation of option interface

func SetClient

func SetClient(c *http.Client) OptionFunc

SetClient option sets client c for request

func SetCookieJar

func SetCookieJar(c http.CookieJar) OptionFunc

SetCookieJar option sets cookie c for request

func SetTimeout

func SetTimeout(t time.Duration) OptionFunc

SetTimeout option sets timeout t for request

func SetTransport

func SetTransport(t *http.Transport) OptionFunc

SetTransport option sets Transport t for request

type Request

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

Request is a request type

func NewRequest

func NewRequest(opts ...Option) *Request

NewRequest returns a new request

func (*Request) AsyncDelete

func (req *Request) AsyncDelete(url string, ch chan<- *Response)

AsyncDelete is a asynchronous delete http request

func (*Request) AsyncGet

func (req *Request) AsyncGet(url string, ch chan<- *Response)

AsyncGet is a asynchronous get http request

func (*Request) AsyncPatch

func (req *Request) AsyncPatch(url string, ch chan<- *Response)

AsyncPatch is a asynchronous patch http request

func (*Request) AsyncPost

func (req *Request) AsyncPost(url string, ch chan<- *Response)

AsyncPost is a asynchronous post http request

func (*Request) AsyncPut

func (req *Request) AsyncPut(url string, ch chan<- *Response)

AsyncPut is a asynchronous put http request

func (*Request) BasicAuth

func (req *Request) BasicAuth(username, password string) *Request

BasicAuth make basic authentication

func (*Request) Body

func (req *Request) Body(formValues []byte) *Request

Body set Post request as body

func (*Request) Delete

func (req *Request) Delete(url string) (*Response, error)

Delete is a delete http request

func (*Request) FormData

func (req *Request) FormData(formValues map[string]string) *Request

FormData set Post request form parameters

func (*Request) Get

func (req *Request) Get(url string) (*Response, error)

Get is a get http request

func (*Request) Head

func (req *Request) Head(url string) (*Response, error)

Head is a head http request

func (*Request) Headers

func (req *Request) Headers(headerVals map[string]string) *Request

Headers set header information

func (*Request) JSON

func (req *Request) JSON(jsonBody map[string]interface{}) *Request

JSON set json data with request

func (*Request) MultipartFormData

func (req *Request) MultipartFormData(formData map[string]string) *Request

MultipartFormData add form data in multipart request

func (*Request) Options

func (req *Request) Options(url string) (*Response, error)

Options is a options http request

func (*Request) Patch

func (req *Request) Patch(url string) (*Response, error)

Patch is a patch http request

func (*Request) Post

func (req *Request) Post(url string) (*Response, error)

Post is a post http request

func (*Request) Put

func (req *Request) Put(url string) (*Response, error)

Put is a put http request

func (*Request) Query

func (req *Request) Query(formValues map[string]string) *Request

Query set request query param

func (*Request) Text

func (req *Request) Text(formValues string) *Request

Text is send text data with post request

func (*Request) Upload

func (req *Request) Upload(name, file string) *Request

Upload upload a single file

func (*Request) Uploads

func (req *Request) Uploads(files map[string]string) *Request

Uploads upload multiple files

type Response

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

Response is a http response struct

func (*Response) GetBody

func (res *Response) GetBody() io.Reader

GetBody returns response body

func (*Response) GetBodyAsByte

func (res *Response) GetBodyAsByte() ([]byte, error)

GetBodyAsByte returns response body as byte

func (*Response) GetBodyAsJSONRawMessage

func (res *Response) GetBodyAsJSONRawMessage() (json.RawMessage, error)

GetBodyAsJSONRawMessage returns response body as json.RawMessage

func (*Response) GetBodyAsString

func (res *Response) GetBodyAsString() (string, error)

GetBodyAsString returns response body as string

func (Response) GetBodyWithUnmarshal

func (res Response) GetBodyWithUnmarshal(v interface{}) error

GetBodyWithUnmarshal unmarshal response body

func (*Response) GetResp

func (res *Response) GetResp() *http.Response

GetResp get net/http original response

func (*Response) GetStatusCode

func (res *Response) GetStatusCode() int

GetStatusCode returns http status code

Jump to

Keyboard shortcuts

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