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即可。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章