gohttp

package
v1.2.27 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

Http 请求封装

  • 并发请求逻辑在开发中
package main

import (
	"fmt"
	gohttpx "github.com/gif-gif/go.io/go-http/go-httpex"
	golog "github.com/gif-gif/go.io/go-log"
	"github.com/gif-gif/go.io/goio"
	"time"
)

func main() {
	goio.Init(goio.DEVELOPMENT)

	req := gohttpx.Request{
		Url: "http://localhost:100",
		Urls: []string{
			"http://localhost:200",
			"http://localhost:300",
			"http://localhost:400",
		},
		QueryParams: map[string]string{"name": "jk"},
		Timeout:     time.Second * 2,
	}
	type httpRequest struct {
		Email string `json:"email"`
	}

	req.Body = &httpRequest{
		Email: "test@gmail.com",
	}

	res := &gohttpx.Response{}
	err := gohttpx.HttpPostJson[gohttpx.Response](&req, res)
	if err != nil {
		golog.ErrorF("Error: %+v\n", err)
	} else {
		fmt.Println(res)
	}

	time.Sleep(10 * time.Second)
}

Documentation

Index

Constants

View Source
const (
	TAG = "gohttp"

	CONTENT_TYPE_XML  = "application/xml"
	CONTENT_TYPE_JSON = "application/json"
	CONTENT_TYPE_FORM = "application/x-www-form-urlencoded"
)

Variables

This section is empty.

Functions

func Get

func Get(url string) ([]byte, error)

func GetClientIp added in v1.1.11

func GetClientIp(r *http.Request) string

GetClientIp returns the client ip of this request without port. Note that this ip address might be modified by client header.

func GetWithQuery

func GetWithQuery(url string, data []byte) ([]byte, error)

func Post

func Post(url string, data []byte) ([]byte, error)

func PostJson

func PostJson(url string, data []byte) ([]byte, error)

func Put

func Put(url string, data []byte) ([]byte, error)

func Upload

func Upload(url, fileField, fileName string, fh io.Reader, data map[string]string) (b []byte, err error)

Types

type Option

type Option struct {
	Name  string
	Value interface{}
}

func ContentTypeFormOption

func ContentTypeFormOption() Option

func ContentTypeJsonOption

func ContentTypeJsonOption() Option

func ContentTypeXmlOption

func ContentTypeXmlOption() Option

func DebugOption

func DebugOption() Option

func HeaderOption

func HeaderOption(field, value string) Option

func TlsOption

func TlsOption(caCrtFile, clientCrtFile, clientKeyFile string) Option

type Request

type Request struct {
	Headers map[string]string
	Tls     *Tls
	// contains filtered or unexported fields
}

func Debug

func Debug() *Request

func New

func New(opts ...Option) *Request

func SetHeader

func SetHeader(name, value string) *Request

func (*Request) Debug

func (r *Request) Debug() *Request

func (*Request) Do

func (r *Request) Do(method, url string, reader io.Reader) (rst []byte, err error)

func (*Request) Get

func (r *Request) Get(url string) ([]byte, error)

func (*Request) GetWithQuery

func (r *Request) GetWithQuery(url string, data []byte) ([]byte, error)

func (*Request) JsonContentType

func (r *Request) JsonContentType() *Request

func (*Request) Post

func (r *Request) Post(url string, data []byte) ([]byte, error)

func (*Request) PostJson

func (r *Request) PostJson(url string, data []byte) ([]byte, error)

func (*Request) Put

func (r *Request) Put(url string, data []byte) ([]byte, error)

func (*Request) SetContentType

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

func (*Request) SetHeader

func (r *Request) SetHeader(name, value string) *Request

func (*Request) SetTimeout

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

func (*Request) Upload

func (r *Request) Upload(url, fileField, fileName string, fh io.Reader, data map[string]string) ([]byte, error)

type Tls

type Tls struct {
	CaCrtFile     string
	ClientCrtFile string
	ClientKeyFile string
}

func (*Tls) CaCrt

func (s *Tls) CaCrt() []byte

func (*Tls) ClientCrt

func (s *Tls) ClientCrt() tls.Certificate

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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