【一、http】go的http基本请求方法

1、http的基本请求

cpp 复制代码
package main

import (
	"bytes"
	"fmt"
	"io"
	"net/http"
	"net/url"
)


func post(){
	r, err := http.Post("http://httpbin.org/post", "", nil)
	if err != nil {
		fmt.Println("ss")
	}
	defer r.Body.Close()
	content, err := io.ReadAll(r.Body)
	if err != nil {
		fmt.Println("ss")
	}
	fmt.Printf("%s", content)
}

func get(){
	r, err := http.Get("http://httpbin.org/get")
	if err != nil {
		fmt.Println("ss")
	}
	defer r.Body.Close()
	content, err := io.ReadAll(r.Body)
	if err != nil {
		fmt.Println("ss")
	}
	fmt.Printf("%s", content)
}

//http只提供了get和post的基本请求,其他的情况不存在,因此,需要自己发起请求,构造方法
func put(){
	requst, err := http.NewRequest(http.MethodPut, "http://httpbin.org/put", nil)
	if err != nil {
		fmt.Println("ss")
	}
	r, err := http.DefaultClient.Do(requst)
	if err != nil {
		fmt.Println("ss")
	}

	defer r.Body.Close()
	content, err := io.ReadAll(r.Body)
	if err != nil {
		fmt.Println("ss")
	}
	fmt.Printf("%s", content)

}

func deletets(){
	requst, err := http.NewRequest(http.MethodDelete, "http://httpbin.org/delete", nil)
	if err != nil {
		fmt.Println("ss")
	}
	r, err := http.DefaultClient.Do(requst)
	if err != nil {
		fmt.Println("ss")
	}

	defer r.Body.Close()
	content, err := io.ReadAll(r.Body)
	if err != nil {
		fmt.Println("ss")
	}
	fmt.Printf("%s", content)

}

其中地址http://httpbin.org 是国外提供的一个验证http请求的网址,可以通过该网站进行测试。

相关推荐
XMYX-09 小时前
37 - Go env 环境变量:配置管理与运行时控制
开发语言·golang
2501_9160074710 小时前
iOS开发中抓取HTTPS请求的完整解决方法与步骤详解
android·网络协议·ios·小程序·https·uni-app·iphone
yqcoder12 小时前
数据的“包装方式”:深入解析 HTTP Content-Type
网络·网络协议·http
wu@5555514 小时前
使用acme生成免费https泛域名证书(通配符证书)
网络协议·http·https
00后程序员张14 小时前
Windows 下怎么生成 AppStoreInfo.plist?不依赖 Xcode 的方法
ide·macos·ios·小程序·uni-app·iphone·xcode
姚不倒14 小时前
Go 进阶实战:实现泛型数据验证器
云原生·golang
wljt15 小时前
为什么要使用Spring Cloud,而不是HTTP直接调用接口?
spring·http·spring cloud
XMYX-016 小时前
36 - Go exec 执行命令
开发语言·golang
lolo大魔王16 小时前
Go 语言 HTTP 协议与 RESTful API 实训全解(理论 + 实战 + 规范)
http·golang·restful
一只小逸白16 小时前
LeetCode Go 常用函数速查表
linux·leetcode·golang