Skip to content

Commit

Permalink
WIP(x/http/client): Implement http.Post() and redirection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
spongehah committed Aug 16, 2024
1 parent 2e9e338 commit ba2a9d0
Show file tree
Hide file tree
Showing 17 changed files with 1,482 additions and 213 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ module github.com/goplus/llgoexamples

go 1.20

require github.com/goplus/llgo v0.9.7-0.20240812013847-321766fd4641
require (
github.com/goplus/llgo v0.9.7-0.20240816085229-53d2d080f4c4
golang.org/x/net v0.28.0
)

require golang.org/x/text v0.17.0 // indirect
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/goplus/llgo v0.9.7-0.20240812013847-321766fd4641 h1:VIJ38bCFRIIr62YXyRKkxy6GXYVA6R3xqAb0HkcoUgw=
github.com/goplus/llgo v0.9.7-0.20240812013847-321766fd4641/go.mod h1:5Fs+08NslqofJ7xtOiIXugkurYOoQvY02ZkFNWA1uEI=
github.com/goplus/llgo v0.9.7-0.20240816085229-53d2d080f4c4 h1:fqqbWhWaoseSplLJF8OTkNGl4Kruqm1wQWT/Yooq6E4=
github.com/goplus/llgo v0.9.7-0.20240816085229-53d2d080f4c4/go.mod h1:5Fs+08NslqofJ7xtOiIXugkurYOoQvY02ZkFNWA1uEI=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
4 changes: 3 additions & 1 deletion x/http/_demo/post/post.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"bytes"
"fmt"
"io"

"github.com/goplus/llgoexamples/x/http"
)

func main() {
resp, err := http.Post("https://jsonplaceholder.typicode.com/posts", "application/json; charset=UTF-8", nil)
data := []byte(`{"id":1,"title":"foo","body":"bar","userId":"1"}`)
resp, err := http.Post("https://jsonplaceholder.typicode.com/posts", "application/json; charset=UTF-8", bytes.NewBuffer(data))
if err != nil {
fmt.Println(err)
return
Expand Down
26 changes: 26 additions & 0 deletions x/http/_demo/redirect/redirect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"io"

"github.com/goplus/llgoexamples/x/http"
)

func main() {
resp, err := http.Get("http://localhost:8080") // Start "../server/redirectServer.go" before running
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resp.Status, "read bytes: ", resp.ContentLength)
fmt.Println(resp.Proto)
resp.PrintHeaders()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
defer resp.Body.Close()
}
26 changes: 26 additions & 0 deletions x/http/_demo/server/redirectServer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"log"
"net/http"
)

func main() {
http.HandleFunc("/", handleInitialRequest)
http.HandleFunc("/redirect", handleRedirectRequest)

fmt.Println("Server is running on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}

func handleInitialRequest(w http.ResponseWriter, r *http.Request) {
log.Println("Received initial request, redirecting...")
http.Redirect(w, r, "/redirect", http.StatusSeeOther)
}

func handleRedirectRequest(w http.ResponseWriter, r *http.Request) {
log.Println("Received redirect request, sending response...")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "Hello redirect")
}
4 changes: 2 additions & 2 deletions x/http/_demo/timeout/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func main() {
client := &http.Client{
Timeout: time.Millisecond, // Set a small timeout to ensure it will time out
//Timeout: time.Second * 5,
//Timeout: time.Millisecond, // Set a small timeout to ensure it will time out
Timeout: time.Second * 5,
}
req, err := http.NewRequest("GET", "https://www.baidu.com", nil)
if err != nil {
Expand Down
Loading

0 comments on commit ba2a9d0

Please sign in to comment.