Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nareix/curl
CURL-like library for golang
https://github.com/nareix/curl
Last synced: 13 days ago
JSON representation
CURL-like library for golang
- Host: GitHub
- URL: https://github.com/nareix/curl
- Owner: nareix
- Created: 2013-06-08T10:00:15.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2018-09-27T16:33:39.000Z (about 6 years ago)
- Last Synced: 2024-08-01T16:18:25.931Z (3 months ago)
- Language: Go
- Homepage:
- Size: 28.3 KB
- Stars: 190
- Watchers: 14
- Forks: 50
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
CURL-like library for golang (NOT libcurl binding)
* Custom HTTP method and header
* Monitoring download progress and speed
* Pause/resume control### Status
[![Travis Build Status](https://travis-ci.org/nareix/curl.svg?branch=master)](https://travis-ci.org/nareix/curl)
### Usage
```go
import "github.com/nareix/curl"req := curl.New("https://kernel.org/pub/linux/kernel/v4.x/linux-4.0.4.tar.xz")
req.Method("POST") // can be "PUT"/"POST"/"DELETE" ...
req.Header("MyHeader", "Value") // Custom header
req.Headers = http.Header { // Custom all headers
"User-Agent": {"mycurl/1.0"},
}ctrl := req.ControlDownload() // Download control
go func () {
// control functions are thread safe
ctrl.Stop() // Stop download
ctrl.Pause() // Pause download
ctrl.Resume() // Resume download
}()req.DialTimeout(time.Second * 10) // TCP Connection Timeout
req.Timeout(time.Second * 30) // Download Timeout// Print progress status per one second
req.Progress(func (p curl.ProgressStatus) {
log.Println(
"Stat", p.Stat, // one of curl.Connecting / curl.Downloading / curl.Closed
"speed", curl.PrettySpeedString(p.Speed),
"len", curl.PrettySizeString(p.ContentLength),
"got", curl.PrettySizeString(p.Size),
"percent", p.Percent,
"paused", p.Paused,
)
}, time.Second)
/*
2015/05/20 15:34:15 Stat 2 speed 0.0B/s len 78.5M got 0.0B percent 0 paused true
2015/05/20 15:34:16 Stat 2 speed 0.0B/s len 78.5M got 0.0B percent 0 paused true
2015/05/20 15:34:16 Stat 2 speed 394.1K/s len 78.5M got 197.5K percent 0.0024564497 paused false
2015/05/20 15:34:17 Stat 2 speed 87.8K/s len 78.5M got 241.5K percent 0.0030038392 paused false
2015/05/20 15:34:17 Stat 2 speed 79.8K/s len 78.5M got 281.5K percent 0.003501466 paused false
2015/05/20 15:34:18 Stat 2 speed 63.9K/s len 78.5M got 313.5K percent 0.0038995675 paused false
*/res, err := req.Do()
res.HttpResponse // related *http.Response struct
log.Println(res.Body) // Body in string
log.Println(res.StatusCode) // HTTP Status Code: 200,404,302 etc
log.Println(res.Hearders) // Reponse headers
log.Println(res.DownloadStatus.AverageSpeed) // Average speed
```