Documentation
¶
Overview ¶
Package netutil implements some basic functions to send http request and get ip info. Note: HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required. HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is: params[0] is header which type should be http.Header or map[string]string, params[1] is query string param which type should be url.Values or map[string]string, when content-type header is multipart/form-data or application/x-www-form-urlencoded params[2] is post body which type should be []byte. params[3] is http client which type should be http.Client.
Index ¶
- func ConvertMapToQueryString(param map[string]any) string
- func EncodeUrl(urlStr string) (string, error)
- func GetInternalIp() string
- func GetIps() []string
- func GetMacAddrs() []string
- func GetRequestPublicIp(req *http.Request) string
- func HttpDelete(url string, params ...any) (*http.Response, error)
- func HttpGet(url string, params ...any) (*http.Response, error)
- func HttpPatch(url string, params ...any) (*http.Response, error)
- func HttpPost(url string, params ...any) (*http.Response, error)
- func HttpPut(url string, params ...any) (*http.Response, error)
- func IsInternalIP(IP net.IP) bool
- func IsPublicIP(IP net.IP) bool
- func ParseHttpResponse(resp *http.Response, obj any) error
- func StructToUrlValues(targetStruct any) url.Values
- type HttpClient
- type HttpClientConfig
- type HttpRequest
- type PublicIpInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertMapToQueryString ¶
ConvertMapToQueryString convert map to sorted url query string
func GetRequestPublicIp ¶ added in v2.1.2
GetRequestPublicIp return the requested public ip
func HttpDelete ¶
HttpDelete send delete http request
func IsInternalIP ¶ added in v2.1.2
IsInternalIP verify an ip is intranet or not
func ParseHttpResponse ¶
ParseHttpResponse decode http response to specified interface
func StructToUrlValues ¶ added in v2.1.6
StructToUrlValues convert struct to url valuse, only convert the field which is exported and has `json` tag
Types ¶
type HttpClient ¶ added in v2.1.6
type HttpClient struct { *http.Client TLS *tls.Config Request *http.Request Config HttpClientConfig }
HttpClient is used for sending http request
func NewHttpClient ¶ added in v2.1.6
func NewHttpClient() *HttpClient
NewHttpClient make a HttpClient instance
func NewHttpClientWithConfig ¶ added in v2.1.6
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
NewHttpClientWithConfig make a HttpClient instance with pass config
func (*HttpClient) DecodeResponse ¶ added in v2.1.6
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
DecodeResponse decode response into target object
func (*HttpClient) SendRequest ¶ added in v2.1.6
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
SendRequest send http request
type HttpClientConfig ¶ added in v2.1.6
type HttpClientConfig struct { SSLEnabled bool TLSConfig *tls.Config Compressed bool HandshakeTimeout time.Duration ResponseTimeout time.Duration Verbose bool }
HttpClientConfig contains some configurations for http client
type HttpRequest ¶ added in v2.1.6
type HttpRequest struct { RawURL string Method string Headers http.Header QueryParams url.Values FormData url.Values Body []byte }
HttpRequest struct is a composed http request
type PublicIpInfo ¶
type PublicIpInfo struct { Status string `json:"status"` Country string `json:"country"` CountryCode string `json:"countryCode"` Region string `json:"region"` RegionName string `json:"regionName"` City string `json:"city"` Lat float64 `json:"lat"` Lon float64 `json:"lon"` Isp string `json:"isp"` Org string `json:"org"` As string `json:"as"` Ip string `json:"query"` }
PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
func GetPublicIpInfo ¶
func GetPublicIpInfo() (*PublicIpInfo, error)
GetPublicIpInfo return public ip information return the PublicIpInfo struct