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

相关推荐
rainFFrain3 小时前
Boost搜索引擎项目(详细思路版)
网络·c++·http·搜索引擎
2501_916007474 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
猿究院--冯磊11 小时前
计算机网络--HTTP协议
网络协议·计算机网络·http
bianshaopeng12 小时前
ubuntu go 环境变量配置
开发语言·ubuntu·golang
元清加油13 小时前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang
让代码飞~13 小时前
idea进阶技能掌握, 使用自带HTTP测试工具,完全可替代PostMan
java·http·intellij-idea·postman
lpfasd12313 小时前
01_Go语言基础与环境搭建
开发语言·后端·golang
Digitally1 天前
重置iPhone会删除所有内容吗? 详细回答
ios·iphone
xy_recording1 天前
Day08 Go语言学习
开发语言·学习·golang