u
This commit is contained in:
1 parent
07e0001b87
commit
46cbb2f50d
543 files changed
+32576
-4231
No files matched your search
@@ -2,6 +2,9 @@ package requestx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3/client"
|
||||
@@ -97,3 +100,31 @@ func PostForm(url string, formData map[string]string, cfg client.Config) (string
|
||||
}
|
||||
return string(resp.Body()), resp.StatusCode(), nil
|
||||
}
|
||||
|
||||
func GetFileStream(ctx context.Context, url string) (io.ReadCloser, int64, string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
req.Header.Set("User-Agent", "Mozilla/5.0")
|
||||
|
||||
httpClient := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
err := resp.Body.Close()
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
return nil, 0, "", fmt.Errorf("request failed: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
return resp.Body, resp.ContentLength, resp.Header.Get("Content-Type"), nil
|
||||
}
|
||||
Reference in new issue
Block a user