发送GET请求
服务端
package main
import (
"zdpgo_api"
)
func ping(c *zdpgo_api.Context) {
c.Writer.WriteString("true")
}
func main() {
api := zdpgo_api.NewApi()
api.Get("/", ping)
// http://localhost:3333
api.Run()
}
package main
import (
"fmt"
"io"
"net/http"
"time"
)
func main() {
// 发送GET请求
c := http.Client
resp, err := c.Get("http://localhost:3333")
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 获取内容
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
发送POST请求
服务端
package main
import (
"zdpgo_api"
)
type LoginRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
func handleLogin(c *zdpgo_api.Context) {
var request LoginRequest
err := c.ShouldBindJSON(&request)
if err != nil {
c.ResponseFailureWithError(err)
return
}
c.ResponseSuccess(request)
}
func main() {
api := zdpgo_api.NewApi()
api.Post("/login", handleLogin)
api.Run()
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"time"
)
func main() {
// 构建请求对象
c := http.Client
// 准备参数
postData := bytes.NewBuffer([]byte(`{"username":"zhangdapeng","password":"zhangdapeng"}`))
// 发送POST请求
resp, err := c.Post("http://localhost:3333/login", "application/json", postData)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 获取内容
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
发送PUT请求
服务端
package main
import (
"zdpgo_api"
)
type updatePasswordRequest struct {
Username string `json:"username"`
OldPassword string `json:"old_password"`
Password string `json:"password"`
RePassword string `json:"re_password"`
}
func handleUpdatePassword(c *zdpgo_api.Context) {
var request updatePasswordRequest
err := c.ShouldBindJSON(&request)
if err != nil {
c.ResponseFailureWithError(err)
return
}
c.ResponseSuccess(request)
}
func main() {
api := zdpgo_api.NewApi()
api.Put("/password", handleUpdatePassword)
api.Run()
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"time"
)
func main() {
// 构建请求对象
c := http.Client
// 准备参数
putData := bytes.NewBuffer([]byte(`{"username":"zhangdapeng","old_password":"zhangdapeng","password":"zhangdapeng1", "re_password":"zhangdapeng1"}`))
// 构建PUT请求
req, err := http.NewRequest("PUT", "http://localhost:3333/password", putData)
if err != nil {
panic(err)
}
req.Header.Add("Accept", `application/json`)
// 发送请求
resp, err := c.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 获取内容
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
发送DELETE请求
服务端
package main
import (
"zdpgo_api"
)
func handleDeleteUser(c *zdpgo_api.Context) {
userId := c.Param("userId")
c.ResponseSuccessWithMsg(userId)
}
func main() {
api := zdpgo_api.NewApi()
api.Delete("/user/:userId", handleDeleteUser)
api.Run()
}
package main
import (
"fmt"
"io"
"net/http"
"time"
)
func main() {
// 构建请求对象
c := http.Client
// 构建DELETE请求
req, err := http.NewRequest("DELETE", "http://localhost:3333/user/1", nil)
if err != nil {
panic(err)
}
req.Header.Add("Accept", `application/json`)
// 发送请求
resp, err := c.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// 获取内容
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
领取专属 10元无门槛券
私享最新 技术干货