Skip to content

Commit

Permalink
doc: add new example to post
Browse files Browse the repository at this point in the history
  • Loading branch information
tonnytg committed Nov 30, 2023
1 parent 895b7e3 commit 66d1125
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 66d1125

Please sign in to comment.