Skip to content

Commit 4be93f8

Browse files
author
Montana Flynn
committed
Add timeout option
1 parent 8e345a2 commit 4be93f8

5 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ module.exports.info = {
192192
| --------------- | ------- | ------------------------------------------------------------------------ |
193193
| `errorChecking` | `false` | add error checking for request, response and body |
194194
| `printBody` | `true` | include code to print the body as a string |
195+
| `timeout` | `-1` | sets a request timeout in seconds (requires go 1.3+) |
195196

196197
### HTTPie
197198

‎src/targets/go/native.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ module.exports = function (options) {
1010
// Define Options
1111
var opts = util._extend({
1212
checkErrors: false,
13-
printBody: true
13+
printBody: true,
14+
timeout: -1
1415
}, options);
1516

1617
// Set some shortcuts
@@ -39,6 +40,7 @@ module.exports = function (options) {
3940
code.push('package main\n');
4041
code.push('import (');
4142
code.push('\t"fmt"');
43+
if (opts.timeout) code.push('\t"time"');
4244
if (bodyPresent) code.push('\t"strings"');
4345
code.push('\t"net/http"');
4446
if (opts.printBody) code.push('\t"io/ioutil"');
@@ -47,13 +49,20 @@ module.exports = function (options) {
4749
code.push('func main() {');
4850

4951
// Create client
50-
code.push('\tclient := &http.Client{}');
52+
if (opts.timeout) {
53+
code.push('\tclient := http.Client{');
54+
code.push('\t\tTimeout: time.Duration(' + opts.timeout + ' * time.Second),');
55+
code.push('\t}');
56+
} else {
57+
code.push('\tclient := &http.Client{}');
58+
}
5159
code.push('\turl := "' + req.url + '"');
5260

5361
// If we have body content or not create the var and reader or nil
5462
if (bodyPresent) {
55-
code.push('\tpayload := ' + JSON.stringify(this.source.postData.text));
56-
req.body = 'strings.NewReader(payload)';
63+
var reqBody = JSON.stringify(this.source.postData.text);
64+
code.push('\tpayload := strings.NewReader(' + reqBody + ')');
65+
req.body = 'payload';
5766
} else {
5867
req.body = 'nil';
5968
}

‎test/fixtures/output/go/native/application-form-encoded.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "foo=bar&hello=world"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("foo=bar&hello=world")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

‎test/fixtures/output/go/native/application-json.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har"
13-
payload := "{\"foo\": \"bar\"}"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("{\"foo\": \"bar\"}")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("Content-Type", "application/json")
1616
res, _ := client.Do(req)
1717
defer res.Body.Close()

‎test/fixtures/output/go/native/full.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
func main() {
1111
client := &http.Client{}
1212
url := "http://mockbin.com/har?baz=abc&foo=bar&foo=baz"
13-
payload := "foo=bar"
14-
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
13+
payload := strings.NewReader("foo=bar")
14+
req, _ := http.NewRequest("POST", url, payload)
1515
req.Header.Add("Accept", "application/json")
1616
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1717
req.Header.Add("Cookie", "foo=bar; bar=baz")

0 commit comments

Comments
 (0)