【一、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请求的网址,可以通过该网站进行测试。

相关推荐
张忠琳6 小时前
【Go 1.26.4】(Part 1) Go 1.26.4 超深度源码分析 — 总体架构与模块全景
开发语言·golang
协享科技10 小时前
Spring Boot 与 Go 双服务架构实践:从单体拆分到通信设计
java·人工智能·spring boot·后端·架构·golang·ai编程
奥利奥夹心脆芙12 小时前
辅助排查 HTTP 接口代码报错,实操完整案例分享
http
周杰伦的稻香13 小时前
Go + Redis:本地部署高性能图片主色调提取服务
开发语言·redis·golang
福大大架构师每日一题13 小时前
2026年6月TIOBE编程语言排行榜,Go语言排名第13,Rust语言排名12。关于Rust已进入平台期的报道似乎为时过早。
开发语言·golang·rust
代码中介商13 小时前
TLS握手全解析:从1.2到1.3的加密演进
网络·网络协议·http
yuegu77714 小时前
HarmonyOS应用<节气通>开发第25篇:HTTP请求封装
网络协议·http·harmonyos
00后程序员张16 小时前
Jenkins 自动上传 IPA 到 App Store 把发布步骤融入 CI/CD
android·ios·小程序·https·uni-app·iphone·webview
serve the people16 小时前
Elasticsearch(5) i want to monitor the es health from a http api
大数据·http·elasticsearch
伊灵eLing16 小时前
GoLang 语言基础
开发语言·后端·golang