Skip to content

Commit 56bc643

Browse files
committed
Merge branch 'master' of https://github.com/montanaflynn/httpsnippet into montanaflynn-master
2 parents 9f36b09 + 3d2724b commit 56bc643

File tree

14 files changed

+293
-0
lines changed

14 files changed

+293
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Relies on the popular [HAR](http://www.softwareishard.com/blog/har-12-spec/#requ
2525
currently the following output [targets](/src/targets) are supported:
2626

2727
- [cURL](http://curl.haxx.se/)
28+
- [Go](http://golang.org/pkg/net/http/#NewRequest)
2829
- [HTTPie](http://httpie.org)
2930
- Node.js
3031
- [Native](http://nodejs.org/api/http.html#http_http_request_options_callback)
@@ -185,6 +186,13 @@ module.exports.info = {
185186
| `short` | `false` | use short form of cURL CLI options |
186187
| `indent` | ` ` | line break & indent output value, set to `false` to disable line breaks |
187188

189+
### Go
190+
191+
| Option | Default | Description |
192+
| --------------- | ------- | ------------------------------------------------------------------------ |
193+
| `errorChecking` | `false` | add error checking for request, response and body |
194+
| `printBody` | `true` | include code to print the body as a string |
195+
188196
### HTTPie
189197

190198
| Option | Default | Description |

src/targets/go/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('require-directory')(module);

src/targets/go/info.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
key: 'go',
5+
title: 'Go',
6+
extname: '.go',
7+
default: 'native'
8+
};

src/targets/go/native.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict';
2+
3+
var util = require('util');
4+
5+
module.exports = function (options) {
6+
7+
// Let's Go!
8+
var code = [];
9+
10+
// Define Options
11+
var opts = util._extend({
12+
checkErrors: false,
13+
printBody: true
14+
}, options);
15+
16+
// Set some shortcuts
17+
var req = {
18+
url: this.source.fullUrl,
19+
method: this.source.method,
20+
hostname: this.source.uriObj.hostname,
21+
port: this.source.uriObj.port,
22+
path: this.source.uriObj.path,
23+
headers: this.source.headersObj
24+
};
25+
26+
var bodyPresent = this.source.postData && this.source.postData.text;
27+
28+
var errorPlaceholder = opts.checkErrors ? 'err' : '_';
29+
30+
var errorCheck = function() {
31+
if (opts.checkErrors) {
32+
code.push('\tif err != nil {');
33+
code.push('\t\tpanic(err)');
34+
code.push('\t}');
35+
}
36+
};
37+
38+
// Create boilerplate
39+
code.push('package main\n');
40+
code.push('import (');
41+
code.push('\t"fmt"');
42+
if (bodyPresent) code.push('\t"strings"');
43+
code.push('\t"net/http"');
44+
if (opts.printBody) code.push('\t"io/ioutil"');
45+
code.push(')\n');
46+
47+
code.push('func main() {');
48+
49+
// Create client
50+
code.push('\tclient := &http.Client{}');
51+
code.push('\turl := "' + req.url + '"');
52+
53+
// If we have body content or not create the var and reader or nil
54+
if (bodyPresent) {
55+
code.push('\tpayload := ' + JSON.stringify(this.source.postData.text));
56+
req.body = 'strings.NewReader(payload)';
57+
} else {
58+
req.body = 'nil';
59+
}
60+
61+
code.push('\treq, ' + errorPlaceholder + ' := http.NewRequest("' + req.method + '", url, ' + req.body + ')');
62+
errorCheck();
63+
64+
// Add headers
65+
var headersPresent = this.source.headers && this.source.headers.length;
66+
67+
if (headersPresent) {
68+
for (var header in this.source.headers) {
69+
var key = this.source.headers[header].name;
70+
var val = this.source.headers[header].value;
71+
code.push('\treq.Header.Add("' + key + '", "' + val + '")');
72+
}
73+
}
74+
75+
// Add Cookies
76+
var cookiesPresent = this.source.cookies && this.source.cookies.length;
77+
if (cookiesPresent) {
78+
var cookies = this.source.cookies.map(function (cookie) {
79+
return cookie.name + '=' + cookie.value;
80+
}).join('; ');
81+
code.push('\treq.Header.Add("Cookie", "' + cookies + '")');
82+
}
83+
84+
// Make request
85+
code.push('\tres, ' + errorPlaceholder + ' := client.Do(req)');
86+
errorCheck();
87+
88+
// Get Body
89+
if (opts.printBody) code.push('\tdefer res.Body.Close()');
90+
if (opts.printBody) code.push('\tbody, ' + errorPlaceholder + ' := ioutil.ReadAll(res.Body)');
91+
errorCheck();
92+
93+
// Print it
94+
code.push('\tfmt.Println(res)');
95+
96+
if (opts.printBody) {
97+
code.push('\tfmt.Println(string(body))');
98+
}
99+
100+
// End main block
101+
code.push('}');
102+
103+
return code.join('\n');
104+
};
105+
106+
module.exports.info = {
107+
key: 'native',
108+
title: 'NewRequest',
109+
link: 'http://golang.org/pkg/net/http/#NewRequest',
110+
description: 'Golang HTTP client request'
111+
};

test/fixtures/available-targets.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,19 @@
8787
"description": "Foundation's NSURLSession request"
8888
}
8989
]
90+
},
91+
{
92+
"key": "go",
93+
"title": "Go",
94+
"extname": ".go",
95+
"default": "native",
96+
"clients": [
97+
{
98+
"key": "native",
99+
"title": "NewRequest",
100+
"link": "http://golang.org/pkg/net/http/#NewRequest",
101+
"description": "Golang HTTP client request"
102+
}
103+
]
90104
}
91105
]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "foo=bar&hello=world"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har"
13+
payload := "{\"foo\": \"bar\"}"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("Content-Type", "application/json")
16+
res, _ := client.Do(req)
17+
defer res.Body.Close()
18+
body, _ := ioutil.ReadAll(res.Body)
19+
fmt.Println(res)
20+
fmt.Println(string(body))
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"io/ioutil"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
url := "http://mockbin.com/har"
12+
req, _ := http.NewRequest("POST", url, nil)
13+
req.Header.Add("Cookie", "foo=bar; bar=baz")
14+
res, _ := client.Do(req)
15+
defer res.Body.Close()
16+
body, _ := ioutil.ReadAll(res.Body)
17+
fmt.Println(res)
18+
fmt.Println(string(body))
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"net/http"
7+
"io/ioutil"
8+
)
9+
10+
func main() {
11+
client := &http.Client{}
12+
url := "http://mockbin.com/har?baz=abc&foo=bar&foo=baz"
13+
payload := "foo=bar"
14+
req, _ := http.NewRequest("POST", url, strings.NewReader(payload))
15+
req.Header.Add("Accept", "application/json")
16+
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
17+
req.Header.Add("Cookie", "foo=bar; bar=baz")
18+
res, _ := client.Do(req)
19+
defer res.Body.Close()
20+
body, _ := ioutil.ReadAll(res.Body)
21+
fmt.Println(res)
22+
fmt.Println(string(body))
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"io/ioutil"
7+
)
8+
9+
func main() {
10+
client := &http.Client{}
11+
url := "http://mockbin.com/har"
12+
req, _ := http.NewRequest("GET", url, nil)
13+
req.Header.Add("Accept", "application/json")
14+
req.Header.Add("X-Foo", "Bar")
15+
res, _ := client.Do(req)
16+
defer res.Body.Close()
17+
body, _ := ioutil.ReadAll(res.Body)
18+
fmt.Println(res)
19+
fmt.Println(string(body))
20+
}

0 commit comments

Comments
 (0)