mirror of
https://github.com/gofiber/fiber.git
synced 2025-04-27 13:14:31 +00:00
* docs: add docs for new client * Add docs for client hooks * Add docs for client examples * Some fixes. * docs: add docs for new client * docs: add docs for new client * Add more examples for methods * Update docs/client/examples.md Co-authored-by: Jason McNeil <sixcolors@mac.com> * Add one more example for cookiejar * apply review * apply review * apply review * docs: add docs for new client * docs: add docs for new client --------- Co-authored-by: René <rene@gofiber.io> Co-authored-by: Jason McNeil <sixcolors@mac.com>
3.4 KiB
3.4 KiB
id, title, description, sidebar_position
id | title | description | sidebar_position |
---|---|---|---|
examples | 🍳 Examples | Client usage examples. | 5 |
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
Basic Auth
package main
import (
"encoding/base64"
"fmt"
"github.com/gofiber/fiber/v3/client"
)
func main() {
cc := client.New()
out := base64.StdEncoding.EncodeToString([]byte("john:doe"))
resp, err := cc.Get("http://localhost:3000", client.Config{
Header: map[string]string{
"Authorization": "Basic " + out,
},
})
if err != nil {
panic(err)
}
fmt.Print(string(resp.Body()))
}
package main
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/basicauth"
)
func main() {
app := fiber.New()
app.Use(
basicauth.New(basicauth.Config{
Users: map[string]string{
"john": "doe",
},
}),
)
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}
TLS
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"github.com/gofiber/fiber/v3/client"
)
func main() {
cc := client.New()
certPool, err := x509.SystemCertPool()
if err != nil {
panic(err)
}
cert, err := os.ReadFile("ssl.cert")
if err != nil {
panic(err)
}
certPool.AppendCertsFromPEM(cert)
cc.SetTLSConfig(&tls.Config{
RootCAs: certPool,
})
resp, err := cc.Get("https://localhost:3000")
if err != nil {
panic(err)
}
fmt.Print(string(resp.Body()))
}
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
err := app.Listen(":3000", fiber.ListenConfig{
CertFile: "ssl.cert",
CertKeyFile: "ssl.key",
})
if err != nil {
panic(err)
}
}
Cookiejar
Request
func main() {
jar := client.AcquireCookieJar()
defer client.ReleaseCookieJar(jar)
cc := client.New()
cc.SetCookieJar(jar)
jar.SetKeyValueBytes("httpbin.org", []byte("john"), []byte("doe"))
resp, err := cc.Get("https://httpbin.org/cookies")
if err != nil {
panic(err)
}
fmt.Println(string(resp.Body()))
}
Click here to see the result
{
"cookies": {
"john": "doe"
}
}
Response
func main() {
jar := client.AcquireCookieJar()
defer client.ReleaseCookieJar(jar)
cc := client.New()
cc.SetCookieJar(jar)
_, err := cc.Get("https://httpbin.org/cookies/set/john/doe")
if err != nil {
panic(err)
}
uri := fasthttp.AcquireURI()
defer fasthttp.ReleaseURI(uri)
uri.SetHost("httpbin.org")
uri.SetPath("/cookies")
fmt.Println(jar.Get(uri))
}
Click here to see the result
[john=doe; path=/]
Response 2
func main() {
jar := client.AcquireCookieJar()
defer client.ReleaseCookieJar(jar)
cc := client.New()
cc.SetCookieJar(jar)
_, err := cc.Get("https://httpbin.org/cookies/set/john/doe")
if err != nil {
panic(err)
}
resp, err := cc.Get("https://httpbin.org/cookies")
if err != nil {
panic(err)
}
fmt.Println(resp.String())
}
Click here to see the result
{
"cookies": {
"john": "doe"
}
}