Categorygithub.com/pkg6/go-requests
modulepackage
0.2.3
Repository: https://github.com/pkg6/go-requests.git
Documentation: pkg.go.dev

# README

Test Status Go Report Card Go.Dev reference Sourcegraph Release

基本介绍

GoRequests框架提供了强大便捷易用的HTTP客户端,基于http.Client进行扩展开发,对象创建可以通过requests.New()包方法,也可以通过new(requests.Client).Clone()方法调用,同时你还可以使用requests.NewHttpClient(requests.DefaultHttpClient(nil))创建对象。推荐使用requests.New()来便捷地创建HTTP客户端对象。

安装

$ go get github.com/pkg6/go-requests

基础使用

package main

import (
	"context"
	"fmt"
	"github.com/pkg6/go-requests"
	"net/url"
)

func main() {
	data := url.Values{}
	data.Set("k", "v")
	get, _ := requests.New().Get(context.Background(), "http://www.httpbin.org/get", data)
	defer get.Close()
	fmt.Println(get.ReadAllString())
	post, _ := requests.New().AsJson().Post(context.Background(), "http://www.httpbin.org/post", data)
	defer post.Close()
	fmt.Println(post.ReadAllString())
}

文件上传

u := url.Values{}
u.Set("服务端接受的name名称", "@file:loaclfile.txt")
request := requests.New()
request.PostForm(context.Background(), "http://127.0.0.1/upload", u)

stream请求与返回处理

ChatGPT【以微软为例】

package main

import (
	"fmt"
	"github.com/pkg6/go-requests"
)

func main() {
	payload := `{
  "messages": [
    {
      "role": "system",
      "content": "You are an AI assistant that helps people find information."
    },
    {
      "role": "user",
      "content": "你是谁"
    }
  ],
  "model":"gpt-35-turbo",
  "temperature": 0.7,
  "top_p": 0.95,
  "frequency_penalty": 0,
  "presence_penalty": 0,
  "max_tokens": 800,
  "stop": null,
  "stream":true 
}`

	json, _ := requests.PostJson("end-point url", payload, func(client *requests.Client) {
		client.WithHeader("api-key", "api-key")
	})
	defer json.Close()
	json.ReadStream(func(line []byte, number int64) {
		fmt.Println(string(line))
	})
}

方法列表

https://pkg.go.dev/github.com/pkg6/go-requests

返回对象

(r *Response) Close() error
(r *Response) TraceInfo() TraceInfo
(r *Response) GetCookie() Cookie
(r *Response) ReadAll() []byte
(r *Response) ReadStream(lineNumberFun func(line []byte, number int64)) int64
(r *Response) ReadAllString() string
(r *Response) ContentType() string
(r *Response) Unmarshal(d any) error
(r *Response) IsSuccess() bool
(r *Response) IsError() bool

加入我们

如果你认可我们的开源项目,有兴趣为 go-requests 的发展做贡献,竭诚欢迎加入我们一起开发完善。无论是报告错误或是 Pull Request 开发,那怕是修改一个错别字也是对我们莫大的帮助。

License

go-requests is licensed under the Apache License 2.0 License - see the LICENSE file for details

# Functions

AnyString converts `any` to string.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
DefaultHttpClient set InsecureSkipVerify = false c.Client.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = false.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
DomainCheckRedirectPolicy is convenient method to define domain name redirect rule in client.
No description provided by the author
FlexibleRedirectPolicy is convenient method to create No of redirect policy for HTTP client.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
HttpBuildQuery Generate get request parameters.
IsJSONType method is to check JSON content type or not.
No description provided by the author
No description provided by the author
IsXMLType method is to check XML content type or not.
No description provided by the author
No description provided by the author
Md5Readerf, _ := os.Open("./_example/1.jpeg")f.Seek(0, 0)requests.Md5Reader(f).
No description provided by the author
No description provided by the author
No description provided by the author
NewFileCache go func() { ticker := time.NewTicker(time.Minute * 5) for range ticker.C { cache.CleanExpired() } }().
No description provided by the author
NewReadCloser creates and returns a RepeatReadCloser object.
No description provided by the author
NoRedirectPolicy is used to disable redirects in the HTTP client requests.WithRedirectPolicy(NoRedirectPolicy()).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Post
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Deprecated: use PostForm instead.
Deprecated: use PostFormUnmarshal instead.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
RandomMobileUserAgent generates a random MOBILE browser user-agent on every requests.
RandomUserAgent generates a random DESKTOP browser user-agent on every requests.
No description provided by the author
No description provided by the author
RequestRoute route := &requests.Route{ Uri: "https://api.github.com/users/github", Method: http.MethodGet, D: &.tests.GitHubUser{}, } _ = requests.RequestRoute(route) fmt.Println(route.GetD().(*.tests.GitHubUser)).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ReadCloser implements the io.ReadCloser interface which is used for reading request body content multiple times.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
MiddlewareFunc middleware handler func.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author