From 66d112567dca1c6d0685087444011d90269e8a9c Mon Sep 17 00:00:00 2001 From: Antonio Thomacelli Gomes Date: Wed, 29 Nov 2023 23:03:36 -0300 Subject: [PATCH] doc: add new example to post --- example/main.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 example/main.go diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..c7b10fe --- /dev/null +++ b/example/main.go @@ -0,0 +1,50 @@ +package main + +import ( + "encoding/json" + "fmt" + "github.com/tonnytg/webreq" + "time" +) + +type Friend struct { + CreatedAt time.Time `json:"createdAt"` + Name string `json:"name"` +} + +func main() { + + headers := webreq.NewHeaders() + headers.Add("Content-Type", "application/json") + if len(headers.Headers) != 1 { + fmt.Println("headers is empty") + } + + f := Friend{ + CreatedAt: time.Now(), + Name: "Tonny", + } + + // convert f to bytes + fBytes, err := json.Marshal(f) + if err != nil { + fmt.Println(err) + } + + request := webreq.NewRequest("POST") + request.SetURL("https://623a666d5f037c136217238f.mockapi.io/api/v1/categories") + request.SetBody(fBytes) + request.SetHeaders(headers.Headers) // Set map directly + request.SetTimeout(10) + + body, err := request.Execute() + if err != nil { + fmt.Println(err) + } + bodyString := string(body) + if bodyString == "" { + fmt.Println("body is empty") + } + + fmt.Println(bodyString) +}