httputil

package
v12.58.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: Apache-2.0 Imports: 2 Imported by: 1

Documentation

Overview

Package httputil provides methods for working with HTTP request/responses

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDescByCode

func GetDescByCode(code int) string

GetDescByCode returns response code description

Example
fmt.Println("200:", GetDescByCode(200))
fmt.Println("404:", GetDescByCode(404))
Output:

200: OK
404: Not Found

func GetPortByScheme

func GetPortByScheme(s string) string

GetPortByScheme returns port for supported scheme

Example
fmt.Println(GetPortByScheme("https"))
Output:

443

func GetRemoteAddr

func GetRemoteAddr(r *http.Request) (string, string)

GetRemoteAddr returns network address that sent the request

Example
r, _ := http.NewRequest("GET", "https://http.cat/200", nil)
r.RemoteAddr = "127.0.0.1:19371"

fmt.Println(GetRemoteAddr(r))
Output:

127.0.0.1 19371

func GetRemoteHost

func GetRemoteHost(r *http.Request) string

GetRemoteHost returns host that sent the request

Example
r, _ := http.NewRequest("GET", "https://http.cat/200", nil)
r.RemoteAddr = "127.0.0.1:19371"

fmt.Println(GetRemoteHost(r))
Output:

127.0.0.1

func GetRemotePort

func GetRemotePort(r *http.Request) string

GetRemotePort returns port of the host that sent the request

Example
r, _ := http.NewRequest("GET", "https://http.cat/200", nil)
r.RemoteAddr = "127.0.0.1:19371"

fmt.Println(GetRemotePort(r))
Output:

19371

func GetRequestAddr

func GetRequestAddr(r *http.Request) (string, string)

GetRequestAddr returns host and port info from request

Example
r, _ := http.NewRequest("GET", "https://http.cat/200", nil)

fmt.Println(GetRequestAddr(r))
Output:

http.cat 443

func GetRequestHost

func GetRequestHost(r *http.Request) string

GetRequestHost returns host from request struct

Example
r, _ := http.NewRequest("GET", "https://http.cat/200", nil)

fmt.Println(GetRequestHost(r))
Output:

http.cat

func GetRequestPort

func GetRequestPort(r *http.Request) string

GetRequestPort returns port from request struct

Example
r, _ := http.NewRequest("GET", "https://http.cat/200", nil)

fmt.Println(GetRequestPort(r))
Output:

443

func IsFTP added in v12.57.0

func IsFTP(url string) bool

IsFTP returns true if given URL contains ftp scheme

Example
url1 := "ftp://domain.com"
url2 := "http://domain.com"

fmt.Printf("%s: %t\n", url1, IsFTP(url1))
fmt.Printf("%s: %t\n", url2, IsFTP(url2))
Output:

ftp://domain.com: true
http://domain.com: false

func IsHTTP added in v12.57.0

func IsHTTP(url string) bool

IsHTTP returns true if given URL contains http scheme

Example
url1 := "https://domain.com"
url2 := "http://domain.com"

fmt.Printf("%s: %t\n", url1, IsHTTP(url1))
fmt.Printf("%s: %t\n", url2, IsHTTP(url2))
Output:

https://domain.com: false
http://domain.com: true

func IsHTTPS added in v12.57.0

func IsHTTPS(url string) bool

IsHTTPS returns true if given URL contains https scheme

Example
url1 := "https://domain.com"
url2 := "http://domain.com"

fmt.Printf("%s: %t\n", url1, IsHTTPS(url1))
fmt.Printf("%s: %t\n", url2, IsHTTPS(url2))
Output:

https://domain.com: true
http://domain.com: false

func IsURL

func IsURL(url string) bool

IsURL checks if given value is valid URL or not

Example
url1 := "https://domain.com"
url2 := "httpj://domain.com"

fmt.Printf("%s: %t\n", url1, IsURL(url1))
fmt.Printf("%s: %t\n", url2, IsURL(url2))
Output:

https://domain.com: true
httpj://domain.com: false

Types

This section is empty.

Jump to

Keyboard shortcuts

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