Skip to content

Latest commit

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

goHttpProMaxPlus

Go quickly request http and Support for AOP

go快速请求Http, 同时支持AOP

要不是公司用go,我才不会写他呢!!!

以下英语均为机翻

Import dependence 导入依赖

go get github.com/971181317/goHttpProMaxPlus

Quick start 快速开始

First build a server with the gin framework

首先用gin框架搭建一个服务器

import"github.com/gin-gonic/gin"r:=gin.Default()
r.GET("/", func(c*gin.Context) {
c.JSON(200, gin.H{
"msg": "This is a GET method",
})
})
r.POST("/", func(c*gin.Context) {
c.JSON(200, gin.H{
"msg": "This is a POST method",
})
})
r.Run() // listen and serve on 0.0.0.0:8080

Next use goHttpProMaxPlus to quickly request Get and Post

接下来使用goHttpProMaxPlus快速请求GetPost

import . "github.com/971181317/goHttpProMaxPlus"resp, err:=GetDefaultClient().Get("http://localhost:8080")
iferr!=nil {
panic(err)
}
fmt.Println(resp.GetRespStr())
resp, err=GetDefaultClient().Post("http://localhost:8080")
iferr!=nil {
panic(err)
}
fmt.Println(resp.GetRespStr())

result

结果

{"msg":"This is a GET method"}
{"msg":"This is a POST method"}

It's pretty easy, right?

芜湖,起飞(老师,老师,我已经会get和post了)!!!

But what if you want to use Header, Cookie, or Form?

但如果想使用HeaderCookie或者Form怎么办呢?

// cookie, header, form: map[string]string// data: *io.ReaderGetDefaultClient().GetWithCookieAndHeader(url, cookie, header)
GetDefaultClient().PostWithForm(url, form)
GetDefaultClient().PostWithCookieHeaderAndForm(url, cookie, header, form)
GetDefaultClient().PostWithIoData(urlsting, data)
GetDefaultClient().PostWithCookieHeaderAndIoData(url, cookie, header, data)

AOP

5 Aspects:

  1. BeforeClientBuild
  2. AfterClientBuild
  3. BeforeRequestBuild
  4. AfterRequestBuild
  5. AfterResponseCreate

Custom HTTP request 自定义请求

4 Steps:

  1. create client
// AspectModel 切片模组typeAspectModelfunc(...interface{})
// HttpClient A client send requesttypeHttpClientstruct {
c*http.ClientBeforeClientBuildAspectModelAfterClientBuildAspectModelBeforeRequestBuildAspectModelAfterRequestBuildAspectModelAfterResponseCreateAspectModelAspectArgs []interface{} // Only the most recent assignment will be kept
}

create it and use aspect

// This method execute BeforeClientBuild and AfterClientBuild.funcNewClientX(client*http.Client,
beforeClientBuild, afterClientBuild, beforeRequestBuild, afterRequestBuild, afterResponseCreateAspectModel,
args...interface{}) *HttpClient
  1. create request
// Forms > Json > Xml > File > ReaderBodytypeHttpRequeststruct {
MethodHttpMethodURLstringCookiesmap[string]stringHeadersmap[string]stringQueriesmap[string]stringFormsmap[string]stringJson*stringXml*stringFile*os.FileReaderBody*io.Reader
}

Two types of assignment

// 1. chainreq:=NewHttpRequest().
SetMethod(POST).
SetURL("http://localhost:8080").
AppendHeader("header", "headerValue").
AppendCookie("cookie", "cookieValue").
AppendForm("form", "formValue").
AppendQuery("query", "queryValue")
// 2.req:=&HttpRequest{
MethodHttpMethodURL "http://localhost:8080"Cookies...Headers...Queries...Forms......
}
  1. do request
resp, err:=client.Do(req)
  1. get request body
typeHttpResponsestruct {
resp*http.ResponseHeadersmap[string]stringbody io.ReadCloserbodyStr*string
}
func (hr*HttpResponse) ParseJson(vinterface{})
func (hr*HttpResponse) GetRespStr() string

About

Go quickly request http and Support for AOP-------go快速请求Http, 同时支持AOP

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages