httputil

package module
v0.0.0-...-906667a Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 18 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

httputil

golang http utils for common use

Basic Usage

Installation

To get the package, execute:

go get github.com/gofika/httputil
Example
package main

import (
	"fmt"

	"github.com/gofika/httputil"
)

func main() {
	resp, err := httputil.Get("https://httpbin.org/get")
	if err != nil {
		panic(err)
	}
	type GetResp struct {
		URL string `json:"url"`
	}
	res, err := httputil.ReadJSON[GetResp](resp)
	fmt.Printf("url=%s\n", res.URL)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(url string, opts ...RequestOption) (resp *http.Response, err error)

Get get

func PostForm

func PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)

PostForm post form

func PostFormFiles

func PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)

PostFormFiles post form files

func PostJSON

func PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PostJSON post json

func ReadAll

func ReadAll(resp *http.Response) (b []byte, err error)

ReadAll read all data from resp

func ReadAnyJSON

func ReadAnyJSON(resp *http.Response, v any) (err error)

ReadAnyJSON read json from resp.Body

func ReadJSON

func ReadJSON[T any](resp *http.Response) (T, error)

ReadJSON read json from resp.Body

func ReadString

func ReadString(resp *http.Response) (s string, err error)

ReadString read string from resp.Body and auto convert encoding

func SaveFile

func SaveFile(resp *http.Response, name string) (written int64, err error)

SaveFile save file from resp.Body

func WithContentType

func WithContentType(contentType string) func(*RequestOptions)

func WithDialTimeout

func WithDialTimeout(dialTimeout time.Duration) func(*ClientOptions)

func WithExpectContinueTimeout

func WithExpectContinueTimeout(expectContinueTimeout time.Duration) func(*ClientOptions)

func WithHeaders

func WithHeaders(headers http.Header) func(*RequestOptions)

func WithIdleConnTimeout

func WithIdleConnTimeout(idleConnTimeout time.Duration) func(*ClientOptions)

func WithKeepAliveTimeout

func WithKeepAliveTimeout(keepAliveTimeout time.Duration) func(*ClientOptions)

func WithMaxIdleConns

func WithMaxIdleConns(maxIdleConns int) func(*ClientOptions)

func WithProxy

func WithProxy(proxy string) func(*ClientOptions)

func WithReferer

func WithReferer(referer string) func(*RequestOptions)

func WithRequestTimeout

func WithRequestTimeout(timeout time.Duration) func(*RequestOptions)

func WithTLSHandshakeTimeout

func WithTLSHandshakeTimeout(tlsHandshakeTimeout time.Duration) func(*ClientOptions)

func WithTimeout

func WithTimeout(timeout time.Duration) func(*ClientOptions)

func WithUserAgent

func WithUserAgent(userAgent string) func(*ClientOptions)

Types

type Client

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

func NewClient

func NewClient(ctx context.Context, opts ...ClientOption) *Client

NewClient new client

func (*Client) Close

func (c *Client) Close()

Close close

func (*Client) Do

func (c *Client) Do(req *http.Request, opts ...RequestOption) (resp *http.Response, err error)

Do do

func (*Client) Get

func (c *Client) Get(url string, opts ...RequestOption) (resp *http.Response, err error)

Get get

func (*Client) Post

func (c *Client) Post(url string, contentType string, body io.Reader, opts ...RequestOption) (resp *http.Response, err error)

Post post

func (*Client) PostForm

func (c *Client) PostForm(url string, data url.Values, opts ...RequestOption) (resp *http.Response, err error)

PostForm post form

func (*Client) PostFormFiles

func (c *Client) PostFormFiles(url string, data url.Values, files []*UploadFile, opts ...RequestOption) (resp *http.Response, err error)

PostFormFiles post form files

func (*Client) PostJSON

func (c *Client) PostJSON(url string, body any, opts ...RequestOption) (resp *http.Response, err error)

PostJSON post json

type ClientOption

type ClientOption func(*ClientOptions)

type ClientOptions

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

ClientOptions http client options

type RequestOption

type RequestOption func(*RequestOptions)

type RequestOptions

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

type UploadFile

type UploadFile struct {
	FieldName string
	FileName  string
	Body      io.Reader
}

UploadFile upload file struct

Jump to

Keyboard shortcuts

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