Last change
on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Line | |
---|
1 | #!/usr/bin/env node
|
---|
2 | var needle = require('./../lib/needle');
|
---|
3 |
|
---|
4 | function exit(code, str) {
|
---|
5 | console.log(str) || process.exit(code);
|
---|
6 | }
|
---|
7 |
|
---|
8 | function usage() {
|
---|
9 | var out = ['Usage: needle [get|head|post|put|delete] url [query]'];
|
---|
10 | out.push('Examples: \n needle get google.com\n needle post server.com/api foo=bar');
|
---|
11 | exit(1, out.join('\n'))
|
---|
12 | }
|
---|
13 |
|
---|
14 | if (process.argv[2] == '-v' || process.argv[2] == '--version')
|
---|
15 | exit(0, needle.version);
|
---|
16 | else if (process.argv[2] == null)
|
---|
17 | usage();
|
---|
18 |
|
---|
19 | var method = process.argv[2],
|
---|
20 | url = process.argv[3],
|
---|
21 | options = { compressed: true, parse_response: true, follow_max: 5, timeout: 10000 };
|
---|
22 |
|
---|
23 | if (!needle[method]) {
|
---|
24 | url = method;
|
---|
25 | method = 'get';
|
---|
26 | }
|
---|
27 |
|
---|
28 | var callback = function(err, resp) {
|
---|
29 | if (err) return exit(1, "Error: " + err.message);
|
---|
30 |
|
---|
31 | if (process.argv.indexOf('-i') != -1)
|
---|
32 | console.log(resp.headers) || console.log('');
|
---|
33 |
|
---|
34 | console.log(resp.body.toString());
|
---|
35 | };
|
---|
36 |
|
---|
37 | if (method == 'post' || method == 'put')
|
---|
38 | needle[method](url, process.argv[4], options, callback);
|
---|
39 | else
|
---|
40 | needle[method](url, options, callback);
|
---|
Note:
See
TracBrowser
for help on using the repository browser.