golang基础-http请求的几种方式

Posted 进击的小猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang基础-http请求的几种方式相关的知识,希望对你有一定的参考价值。

文章目录


简单粗暴,直接上代码

发送get请求


func main() 
	http.HandleFunc("/test1",test1)

	http.HandleFunc("/t_test1",t_test1)

	http.ListenAndServe("0.0.0.0:9999",nil)


func t_test1(res http.ResponseWriter,req *http.Request)  
	//构造URL
	sendUrl,_:=url.Parse("http://localhost:9999/test1?")
	//添加Values键值对
	val:=sendUrl.Query()
	val.Set("name","safly")
	val.Add("name","saflyer")
	val.Set("sex","man")
	//添加RawQuery
	sendUrl.RawQuery = val.Encode()
	//发起请求
	resp,err:=http.Get(sendUrl.String())
	if err != nil || resp.StatusCode != http.StatusOK
		fmt.Println("get fail:", err)
		return
	

	defer resp.Body.Close()

	//读取响应
	body,err:=ioutil.ReadAll(resp.Body)
	if err!= nil
		fmt.Println("read body fail")
		return
	
	fmt.Println(string(body))



func test1(res http.ResponseWriter,req *http.Request)  
	req.ParseForm()
	for k,v:=range  req.Form
		fmt.Print(k,v)
	
	fmt.Fprint(res, "It works")


通过curl -X GET 'http://localhost:9999/t_test1'执行请求测试,输出如下:

name[safly saflyer]sex[man]It works

解析post的方式解析json对象

func main() 
	http.HandleFunc("/post",post)
	http.HandleFunc("/t_post",t_post)
	http.ListenAndServe("0.0.0.0:9999",nil)


func t_post(res http.ResponseWriter,req *http.Request)  
	//构造URL
	sendUrl:="http://localhost:9999/post?"
	user:= struct 
		Name string
		Sex string
	"safly","man"

	body,_:=json.Marshal(user)
	resp,_:= http.Post(sendUrl,"application/x-www-form-urlencoded",strings.NewReader(string(body)))
	//解析响应数据
	defer resp.Body.Close()
	b,_:= ioutil.ReadAll(resp.Body)
	fmt.Println(string(b))


func post(res http.ResponseWriter,req *http.Request)  
	//读取body
	body,_:= ioutil.ReadAll(req.Body)
	strBody:= string(body)
	fmt.Println("bodyStr",strBody)
	userT:= struct 
		Name string
		Sex string
	
	json.Unmarshal(body,&userT)

	fmt.Fprint(res,userT.Name,userT.Sex)

通过curl -X POST 'http://localhost:9999/t_post'执行请求测试,输出如下:

bodyStr "Name":"safly","Sex":"man"
saflyman

解析以表单的方式提交postform数据

func main() 
	http.HandleFunc("/post",post)

	http.HandleFunc("/t_post",t_post)

	http.ListenAndServe("0.0.0.0:9999",nil)


func post(w http.ResponseWriter,r *http.Request) 
	r.ParseForm()
	fmt.Println(r.Form["name"])
	fmt.Println(r.Form["sex"])

	//返回数据
	fmt.Fprint(w, "Post is works")



func t_post(res http.ResponseWriter,req *http.Request)  
	u, err := url.Parse("http://localhost:9999/post?")
	if err != nil 
		fmt.Println("parse is fail")
		return
	
	q := u.Query()
	q.Set("name", "salfy")
	q.Add("name","saflyer")
	q.Set("sex", "男")
	//发起post表单请求
	resp, err1 := http.PostForm(u.String(), q)
	if err1 != nil 
		fmt.Println("post fail")
		return
	
	//解析响应
	defer resp.Body.Close()
	body, err2 := ioutil.ReadAll(resp.Body)
	if err2 != nil
		fmt.Println("body fail")
		return
	
	fmt.Println(string(body))

通过curl -X POST 'http://localhost:9999/t_post'执行请求测试,输出如下:

[salfy saflyer]
[男]
Post is works

以上是关于golang基础-http请求的几种方式的主要内容,如果未能解决你的问题,请参考以下文章

golang中发送http请求的几种常见情况

Golang读写文件的几种方式

PHP发送HTTP请求的几种方式

httpclient发送请求的几种方式

Golang 退出 goroutine的几种方式

golang读写文件的几种方式