Giter VIP home page Giter VIP logo

go-requests's Introduction

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

go-requests's People

Contributors

izhiqiang avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.