Skip to content

Commit c813e02

Browse files
author
Montana Flynn
committed
Print response body
1 parent ae2594d commit c813e02

File tree

8 files changed

+51
-14
lines changed

8 files changed

+51
-14
lines changed

src/targets/go/native.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

3-
// var util = require('util');
3+
var util = require('util');
44

55
module.exports = function (options) {
66

7-
// Might want to add error checking in as option
8-
9-
// var opts = util._extend({
10-
// errorChecking: false
11-
// }, options);
7+
var opts = util._extend({
8+
errorChecking: false,
9+
timeout: -1,
10+
printBody: true
11+
}, options);
1212

1313
// Set some shortcuts
1414
var req = {
@@ -31,6 +31,7 @@ module.exports = function (options) {
3131
code.push('\t"fmt"');
3232
if (bodyPresent) code.push('\t"strings"');
3333
code.push('\t"net/http"');
34+
code.push('\t"io/ioutil"');
3435
code.push(')\n');
3536

3637
code.push('func main() {');
@@ -72,9 +73,17 @@ module.exports = function (options) {
7273
// Make request
7374
code.push('\tres, _ := client.Do(req)');
7475

76+
// Get Body
77+
code.push('\tdefer res.Body.Close()');
78+
code.push('\tbody, _ := ioutil.ReadAll(res.Body)');
79+
7580
// Print it
76-
code.push('\tfmt.Printf("%+v", res)');
81+
code.push('\tfmt.Println(res)');
7782

83+
if (opts.printBody) {
84+
code.push('\tfmt.Println(string(body))');
85+
}
86+
7887
// End main block
7988
code.push('}');
8089

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"net/http"
7+
"io/ioutil"
78
)
89

910
func main() {
@@ -13,5 +14,8 @@ func main() {
1314
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
1415
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1516
res, _ := client.Do(req)
16-
fmt.Printf("%+v", res)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
1721
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"net/http"
7+
"io/ioutil"
78
)
89

910
func main() {
@@ -13,5 +14,8 @@ func main() {
1314
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
1415
req.Header.Add("Content-Type", "application/json")
1516
res, _ := client.Do(req)
16-
fmt.Printf("%+v", res)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
1721
}

test/fixtures/output/go/native/cookies.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"io/ioutil"
67
)
78

89
func main() {
@@ -11,5 +12,8 @@ func main() {
1112
req, _ := http.NewRequest("POST", url, nil)
1213
req.Header.Add("Cookie", "foo=bar; bar=baz")
1314
res, _ := client.Do(req)
14-
fmt.Printf("%+v", res)
15+
defer res.Body.Close()
16+
body, _ := ioutil.ReadAll(res.Body)
17+
fmt.Println(res)
18+
fmt.Println(string(body))
1519
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"strings"
66
"net/http"
7+
"io/ioutil"
78
)
89

910
func main() {
@@ -15,5 +16,8 @@ func main() {
1516
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
1617
req.Header.Add("Cookie", "foo=bar; bar=baz")
1718
res, _ := client.Do(req)
18-
fmt.Printf("%+v", res)
19+
defer res.Body.Close()
20+
body, _ := ioutil.ReadAll(res.Body)
21+
fmt.Println(res)
22+
fmt.Println(string(body))
1923
}

test/fixtures/output/go/native/headers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"io/ioutil"
67
)
78

89
func main() {
@@ -12,5 +13,8 @@ func main() {
1213
req.Header.Add("Accept", "application/json")
1314
req.Header.Add("X-Foo", "Bar")
1415
res, _ := client.Do(req)
15-
fmt.Printf("%+v", res)
16+
defer res.Body.Close()
17+
body, _ := ioutil.ReadAll(res.Body)
18+
fmt.Println(res)
19+
fmt.Println(string(body))
1620
}

test/fixtures/output/go/native/query.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"io/ioutil"
67
)
78

89
func main() {
910
client := &http.Client{}
1011
url := "http://mockbin.com/har?key=value&baz=abc&foo=bar&foo=baz"
1112
req, _ := http.NewRequest("GET", url, nil)
1213
res, _ := client.Do(req)
13-
fmt.Printf("%+v", res)
14+
defer res.Body.Close()
15+
body, _ := ioutil.ReadAll(res.Body)
16+
fmt.Println(res)
17+
fmt.Println(string(body))
1418
}

test/fixtures/output/go/native/short.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"io/ioutil"
67
)
78

89
func main() {
910
client := &http.Client{}
1011
url := "http://mockbin.com/har"
1112
req, _ := http.NewRequest("GET", url, nil)
1213
res, _ := client.Do(req)
13-
fmt.Printf("%+v", res)
14+
defer res.Body.Close()
15+
body, _ := ioutil.ReadAll(res.Body)
16+
fmt.Println(res)
17+
fmt.Println(string(body))
1418
}

0 commit comments

Comments
 (0)