httpreq

package module
v0.0.0-...-4d74056 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

httpreq

httpreq is an http request library written with Golang to make requests and handle responses easily.

Install

  go get github.com/binalyze/httpreq

Overview

httpreq implements a friendly API over Go's existing net/http library.

Req and Response are two most important struct. You can think of Req as a client that initiate HTTP requests, Resp as a information container for the request and response. They all provide simple and convenient APIs that allows you to do a lot of things.

req := httpreq.New(url)
resp, err := req.Get()

Roadmap

  • Support query parameters
  • Support cookies
  • Support XML
  • Support proxy
  • Configurable transport

Usage

Here is an example to use some helper methods of httpreq. You can find more examples in test files.

Request
  // Create new request
  req := httpreq.New("https://your-address-to-send-json.com")

  // Set Timeout
  req.SetTimeout(30 * time.Second)

  // Set Header (i.e. JWT Bearer Token)
  var bearer = "Bearer " + <ACCESS TOKEN HERE>
  req.SetHeaders(map[string]string{"Authorization": bearer})

  // Set Content Type
  req.SetContentType("application/json") 

  // Set JSON raw body
  info := &Data{
    FirstName: "John",
    LastName:  "Doe",
    Age:       42,
  }

  jsonData, _ := json.Marshal(info)
	
  req.SetBody(jsonData)

  // Send Request
  resp, _ := req.Post()
Response
  // Body
  result, err := resp.Body()

  // Original response
  response := resp.Response()

  // StatusCode
  statusCode := resp.StatusCode()

  // Headers
  headers := resp.Headers()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuiltinLogger

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

func NewBuiltinLogger

func NewBuiltinLogger() *BuiltinLogger

func (*BuiltinLogger) Debug

func (l *BuiltinLogger) Debug(args ...interface{})

func (*BuiltinLogger) Debugf

func (l *BuiltinLogger) Debugf(format string, args ...interface{})

func (*BuiltinLogger) Error

func (l *BuiltinLogger) Error(args ...interface{})

func (*BuiltinLogger) Errorf

func (l *BuiltinLogger) Errorf(format string, args ...interface{})

func (*BuiltinLogger) Fatal

func (l *BuiltinLogger) Fatal(args ...interface{})

func (*BuiltinLogger) Fatalf

func (l *BuiltinLogger) Fatalf(format string, args ...interface{})

func (*BuiltinLogger) Info

func (l *BuiltinLogger) Info(args ...interface{})

func (*BuiltinLogger) Infof

func (l *BuiltinLogger) Infof(format string, args ...interface{})

func (*BuiltinLogger) Warn

func (l *BuiltinLogger) Warn(args ...interface{})

func (*BuiltinLogger) Warnf

func (l *BuiltinLogger) Warnf(format string, args ...interface{})

type Logger

type Logger interface {
	Debug(args ...interface{})
	Debugf(format string, args ...interface{})
	Info(args ...interface{})
	Infof(format string, args ...interface{})
	Warn(args ...interface{})
	Warnf(format string, args ...interface{})
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}

type Req

type Req struct {
	Params *url.Values
	// contains filtered or unexported fields
}

Req is main struct for requests

func New

func New(ctx context.Context, address string) *Req

New creates a new HTTP Request

func (*Req) Delete

func (r *Req) Delete() (*Response, error)

Delete is a delete http request

func (*Req) Get

func (r *Req) Get() (*Response, error)

Get is a get http request

func (*Req) Post

func (r *Req) Post() (*Response, error)

Post is a post http request

func (*Req) PostJSON

func (r *Req) PostJSON() (*Response, error)

PostJSON is a POST http request as JSON

func (*Req) Put

func (r *Req) Put() (*Response, error)

Put is a put http request

func (*Req) SetBody

func (r *Req) SetBody(data []byte) *Req

SetBody sets request body

func (*Req) SetBodyXML

func (r *Req) SetBodyXML() *Req

SetBodyXML sets content type as XML.

func (*Req) SetContentType

func (r *Req) SetContentType(contentType string) *Req

SetContentType sets content type of request

func (*Req) SetCookie

func (r *Req) SetCookie(c *http.Cookie) *Req

SetCookie sets a cookie to the request

func (*Req) SetForm

func (r *Req) SetForm(files []map[string]string, fields []map[string]string) *Req

SetForm creates form and add files and data to form.

func (*Req) SetHeaders

func (r *Req) SetHeaders(headers map[string]string) *Req

SetHeaders sets request headers

func (*Req) SetParam

func (r *Req) SetParam(param, value string) *Req

func (*Req) SetParams

func (r *Req) SetParams(queryParams map[string]string) *Req

SetParams for set multi or single parameter.

func (*Req) SetProxy

func (r *Req) SetProxy(u string) *Req

SetProxy sets proxy URL to http client

func (*Req) SetTLSConfig

func (r *Req) SetTLSConfig(c *tls.Config) *Req

SetTLSConfig changes the request TLS client configuration

func (*Req) SetTimeout

func (r *Req) SetTimeout(d time.Duration) *Req

SetTimeout changes the request timeout

func (*Req) SetTransport

func (r *Req) SetTransport(transport *http.Transport) *Req

SetTransport sets transport configuration of request

type Response

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

Response is the main struct which holds the http.Response and data.

func (*Response) Body

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

Body returns the response body

func (*Response) Close

func (r *Response) Close() error

Close closes the http.Response

func (*Response) DownloadFile

func (r *Response) DownloadFile(downloadDir string) (contentType string, filePath string, err error)

DownloadFile looks for Content-Disposition header to find the filename attribute and returns the content-type header with saved file path that is saved under given downloadDir.

func (*Response) Headers

func (r *Response) Headers() http.Header

Headers returns the headers of the response

func (*Response) Response

func (r *Response) Response() *http.Response

Response returns the original http.Response

func (*Response) SaveFile

func (r *Response) SaveFile(filePath string) error

SaveFile reads body and then saves the file defined in body

func (*Response) StatusCode

func (r *Response) StatusCode() int

StatusCode returns the status code of the response

Jump to

Keyboard shortcuts

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