首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Go语言发送HTTP请求快速入门

发送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))

}

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20230320A06VLJ00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券