Go框架进阶—— Resty

Simple HTTP and REST client library for Go。简单理解 java里的HttpClient,用于发送http 或rest协议的请求。

github:https://github.com/go-resty/resty

api doc:https://godoc.org/github.com/go-resty/resty

应用实例:

import 	"github.com/go-resty/resty/v2"

	func main(){
		//创建resty client实例
		client := resty.New()

		//1 get url访问
		resp, err := client.R().EnableTrace().Get("https://httpbin.org/get")

		//2 get 设置参数map 访问
		resp, err := client.R().
			SetQueryParams(map[string]string{
				"page_no": "1",
				"limit":   "20",
			}).
			SetHeader("Accept", "application/json").
			SetAuthToken("BC59490").
			Get("/search_result")

		//3 get 设置参数string 访问
		resp, err := client.R().
			SetQueryString("productId=232&template=fresh-sample").
			SetHeader("Accept", "application/json").
			SetAuthToken("BC594900518B4F").
			Get("/show_product")
		
        //4.。。。Post setBody
	}

resty支持GET, POST, PUT, DELETE, HEAD, PATCH, OPTIONS, etc.请求。对应简洁的api设置请求参数。另外还可模拟文件上传下载、请求认证、重试等。这里不赘述,所有的应用都在doc中,需要时查doc即可。

 

你可能感兴趣的:(【Go】)