项目作者: monaco-io

项目描述 :
go request, go http client
高级语言: Go
项目地址: git://github.com/monaco-io/request.git
创建时间: 2020-03-25T06:24:18Z
项目社区:https://github.com/monaco-io/request

开源协议:MIT License

下载


Request Mentioned in Awesome Go Go Report Card Go

GoDoc
codecov
Release
TODOs
License

HTTP Client for golang, Inspired by Javascript-axios Python-request.
If you have experience about axios or requests, you will love it.
No 3rd dependency.

Features

  • Make http requests from Golang
  • Transform request and response data

Installing

go mod:

  1. go get github.com/monaco-io/request

Methods

  • OPTIONS
  • GET
  • HEAD
  • POST
  • PUT
  • DELETE
  • TRACE
  • CONNECT

Example

POST

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. var body = struct {
  7. A string
  8. B int
  9. }{A: "A", B: 1}
  10. var result interface{}
  11. c := request.Client{
  12. URL: "https://google.com",
  13. Method: "POST",
  14. Query: map[string]string{"hello": "world"},
  15. JSON: body,
  16. }
  17. resp := c.Send().Scan(&result)
  18. if !resp.OK(){
  19. // handle error
  20. log.Println(resp.Error())
  21. }
  22. // str := resp.String()
  23. // bytes := resp.Bytes()

POST with local files

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. c := request.Client{
  7. URL: "https://google.com",
  8. Method: "POST",
  9. Query: map[string]string{"hello": "world"},
  10. MultipartForm: MultipartForm{
  11. Fields: map[string]string{"a": "1"},
  12. Files: []string{"doc.txt"},
  13. },
  14. }
  15. resp := c.Send().Scan(&result)
  16. ...

POST step by step

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. var response interface{}
  7. resp := request.
  8. New().
  9. POST("http://httpbin.org/post").
  10. AddHeader(map[string]string{"Google": "google"}).
  11. AddBasicAuth("google", "google").
  12. AddURLEncodedForm(map[string]string{"data": "google"}).
  13. Send().
  14. Scan(&response)
  15. ...

POST with context (1/2)

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. "context"
  5. )
  6. func main() {
  7. c := request.Client{
  8. Context: context.Background(),
  9. URL: "https://google.com",
  10. Method: "POST",
  11. BasicAuth: request.BasicAuth{
  12. Username: "google",
  13. Password: "google",
  14. },
  15. }
  16. resp := c.Send()
  17. ...

POST with context (2/2)

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. "context"
  5. )
  6. func main() {
  7. var response interface{}
  8. resp := request.
  9. NewWithContext(context.TODO()).
  10. POST("http://httpbin.org/post").
  11. AddHeader(map[string]string{"Google": "google"}).
  12. AddBasicAuth("google", "google").
  13. AddURLEncodedForm(map[string]string{"data": "google"}).
  14. Send().
  15. Scan(&response)
  16. ...

Authorization

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. c := request.Client{
  7. URL: "https://google.com",
  8. Method: "POST",
  9. BasicAuth: request.BasicAuth{
  10. Username: "google",
  11. Password: "google",
  12. },
  13. }
  14. resp := c.Send()
  15. }

Timeout

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. c := request.Client{
  7. URL: "https://google.com",
  8. Method: "POST",
  9. Timeout: time.Second*10,
  10. }
  11. }

Cookies

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. c := request.Client{
  7. URL: "https://google.com",
  8. CookiesMap: map[string]string{
  9. "cookie_name": "cookie_value",
  10. }
  11. }
  12. }

TLS

  1. package main
  2. import (
  3. "github.com/monaco-io/request"
  4. )
  5. func main() {
  6. c := request.Client{
  7. URL: "https://google.com",
  8. TLSConfig: &tls.Config{InsecureSkipVerify: true},
  9. }
  10. }

License

MIT