Tải lên tệp Đa phần
Tải lên tệp qua yêu cầu POST Đa phần
// Đọc trước hai tệp
profileImgBytes, _ := os.ReadFile("/Users/jeeva/test-img.png")
notesBytes, _ := os.ReadFile("/Users/jeeva/text-file.txt")
// Tạo một client Resty
client := resty.New()
// Sử dụng SetFileReader để thiết lập file và các tham số tự động cho việc tải lên
// Sử dụng SetFormData để thiết lập các tham số form khác
resp, err := client.R().
SetFileReader("profile_img", "test-img.png", bytes.NewReader(profileImgBytes)).
SetFileReader("notes", "text-file.txt", bytes.NewReader(notesBytes)).
SetFormData(map[string]string{
"first_name": "Jeevanandam",
"last_name": "M",
}).
Post("http://www.tizi365.com/upload")
Tải lên các Tệp Cục bộ Trực tiếp
Điều này tiện lợi hơn phía trên, vì nó có thể tải lên các tệp cục bộ trực tiếp mà không cần phải đọc tệp trước.
// Tạo một client Resty
client := resty.New()
// Sử dụng SetFile để thiết lập tệp cần tải lên và trường tham số yêu cầu liên kết
resp, err := client.R().
SetFile("profile_img", "/Users/jeeva/test-img.png").
Post("http://myapp.com/upload")
// Tải lên tệp một cách hàng loạt sử dụng SetFiles
resp, err := client.R().
SetFiles(map[string]string{
"profile_img": "/Users/jeeva/test-img.png",
"notes": "/Users/jeeva/text-file.txt",
}).
Post("http://myapp.com/upload")
// Tải lên tệp hàng loạt và thiết lập các tham số form khác trong cùng một lúc
resp, err := client.R().
SetFiles(map[string]string{
"profile_img": "/Users/jeeva/test-img.png",
"notes": "/Users/jeeva/text-file.txt",
}).
SetFormData(map[string]string{
"first_name": "Jeevanandam",
"last_name": "M",
"zip_code": "00001",
"city": "thành phố của tôi",
"access_token": "C6A79608-782F-4ED0-A11D-BD82FAD829CD",
}).
Post("http://myapp.com/profile")
Tải xuống Tệp
// Tạo một client Resty
client := resty.New()
// Thiết lập thư mục lưu trữ tệp, Resty sẽ tự động tạo nó nếu nó không tồn tại
// Điều này là tùy chọn nếu SetOutput được thiết lập với một đường dẫn tuyệt đối.
client.SetOutputDirectory("/Users/tizi365/Downloads")
// Gửi một yêu cầu HTTP và lưu kết quả yêu cầu vào đường dẫn tệp được thiết lập bởi tham số SetOutput
_, err := client.R().
SetOutput("plugin/ReplyWithHeader-v5.1-beta.zip").
Get("http://bit.ly/1LouEKr")
// Đây là một ví dụ sử dụng một đường dẫn tuyệt đối
_, err := client.R().
SetOutput("/MyDownloads/plugin/ReplyWithHeader-v5.1-beta.zip").
Get("http://bit.ly/1LouEKr")