Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🛠 greq is a simple http client request builder and sender.

License

NotificationsYou must be signed in to change notification settings

gookit/greq

Repository files navigation

GitHub go.mod Go versionGitHub tag (latest SemVer)GoDocGo Report CardUnit-TestsCoverage Status

中文说明 |English

greq A simple http client request builder and sender

Features

  • Make http requests, supportsGET,POST,PUT,PATCH,DELETE,HEAD
  • Transform request and response data
  • Supports chain configuration request
  • Supports defining and adding middleware
  • Supports defining request body provider and response decoder
  • Built-In: fom, json request body provider
  • Built-In: xml, json response body decoder

Install

go get github.com/gookit/greq

Quick start

package mainimport ("github.com/gookit/goutil/dump""github.com/gookit/greq")funcmain() {resp,err:=greq.New("https://httpbin.org").JSONType().UserAgent("custom-client/1.0").PostDo("/post",`{"name": "inhere"}`)iferr!=nil {panic(err)}ret:=make(map[string]any)err=resp.Decode(&ret)iferr!=nil {panic(err)}dump.P(ret)}

Result:

PRINT AT github.com/gookit/greq_test.TestHReq_Send(greq_test.go:73)map[string]interface {} { #len=4  "args": map[string]interface {} { #len=0  },  "headers": map[string]interface {} { #len=4    "Host": string("httpbin.org"), #len=11    "User-Agent": string("custom-client/1.0"), #len=17    "X-Amzn-Trace-Id": string("Root=1-61e4d41e-06e27ae12ff872a224373ca7"), #len=40    "Accept-Encoding": string("gzip"), #len=4  },  "origin": string("222.210.59.218"), #len=14  "url": string("https://httpbin.org/post"), #len=24},

Request headers

greq.New("some.host/api").SetHeader("req-id","a string")

Set multi at once:

greq.New("some.host/api").SetHeaders(map[string]string{"req-id":"a string",})

Set content type

greq.New("some.host/api").ContentType("text/html")

Built inJSONType()FromType()XMLType()

greq.New("some.host/api").JSONType()

Use middleware

buf:=&bytes.Buffer{}mid0:=greq.MiddleFunc(func(r*http.Request,next greq.HandleFunc) (*greq.Response,error) {buf.WriteString("MID0>>")w,err:=next(r)buf.WriteString(">>MID0")returnw,err})mid1:=greq.MiddleFunc(func(r*http.Request,next greq.HandleFunc) (*greq.Response,error) {buf.WriteString("MID1>>")w,err:=next(r)buf.WriteString(">>MID1")returnw,err})mid2:=greq.MiddleFunc(func(r*http.Request,next greq.HandleFunc) (*greq.Response,error) {buf.WriteString("MID2>>")w,err:=next(r)buf.WriteString(">>MID2")returnw,err})resp,err:=greq.New("https://httpbin.org").Doer(httpreq.DoerFunc(func(req*http.Request) (*http.Response,error) {tw:=httptest.NewRecorder()buf.WriteString("(CORE)")returntw.Result(),nil})).Middleware(mid0,mid1,mid2).GetDo("/get")fmt.Println(buf.String())

Output:

MID2>>MID1>>MID0>>(CORE)>>MID0>>MID1>>MID2

More usage

Check response

  • Response.IsOK() bool
  • Response.IsFail() bool
  • Response.IsEmptyBody() bool
  • Response.IsJSONType() bool
  • Response.IsContentType(prefix string) bool

Get response data

  • Response.ContentType() string
  • Response.Decode(ptr any) error

Request to string

str:=greq.New("https://httpbin.org").UserAgent("some-client/1.0").BasicAuth("inhere","some string").JSONType().Body("hi, with body").Post("/post").String()fmt.Println(str)

Output:

POST https://httpbin.org/post/ HTTP/1.1User-Agent: some-client/1.0Authorization: Basic aW5oZXJlOnNvbWUgc3RyaW5nContent-Type: application/json; charset=utf-8hi, with body

Response to string

greq.Response.String() can convert response to string.

package mainimport ("fmt""github.com/gookit/goutil/dump""github.com/gookit/greq")funcmain() {resp,err:=greq.New("https://httpbin.org").UserAgent("custom-client/1.0").Send("/get")iferr!=nil {panic(err)}fmt.Print(resp.String())}

Output:

HTTP/2.0 200 OKAccess-Control-Allow-Origin: *Access-Control-Allow-Credentials: trueDate: Tue, 18 Jan 2022 04:52:39 GMTContent-Type: application/jsonContent-Length: 272Server: gunicorn/19.9.0{  "args": {},   "headers": {    "Accept-Encoding": "gzip",     "Host": "httpbin.org",     "User-Agent": "custom-client/1.0",     "X-Amzn-Trace-Id": "Root=1-61e64797-3e428a925f7709906a8b7c01"  },   "origin": "222.210.59.218",   "url": "https://httpbin.org/get"}

Refers

About

🛠 greq is a simple http client request builder and sender.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp