forked from microsoft/typed-rest-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.ts
120 lines (103 loc) · 4.52 KB
/
http.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import * as httpm from 'typed-rest-client/HttpClient';
import * as fs from 'fs';
import * as path from 'path';
import * as cm from './common';
let sampleFilePath: string = path.join(process.cwd(), 'httpClientStreamSample.txt');
let httpc: httpm.HttpClient = new httpm.HttpClient('vsts-node-api');
export async function run() {
try {
cm.banner('Http Samples');
//
// Http get. Can await get response.
// The response offers a message (IncomingMessage) and
// an awaitable readBody() which reads the stream to end
//
cm.heading('get request output body');
let res: httpm.HttpClientResponse = await httpc.get('https://httpbin.org/get');
let body: string = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
//
// Http get request reading body to end in a single line
//
cm.heading('get request in a single line');
body = await (await httpc.get('https://httpbin.org/get')).readBody();
cm.outputHttpBinResponse(body);
//
// Http get request using a proxy
//
cm.heading('get request using the proxy url set in the env variables');
const proxySettings = {
proxyUrl: cm.getEnv('PROXY_URL'),
proxyUsername: cm.getEnv('PROXY_USERNAME'),
proxyPassword: cm.getEnv('PROXY_PASSWORD'),
proxyBypassHosts: cm.getEnv('PROXY_BYPASS_HOSTS') ? cm.getEnv('PROXY_BYPASS_HOSTS').split(', ') : null
}
if (proxySettings.proxyUrl) {
httpc = new httpm.HttpClient('vsts-node-api', null, { proxy: proxySettings });
body = await (await httpc.get('https://httpbin.org/get')).readBody();
cm.outputHttpBinResponse(body);
}
else {
console.log("No proxy url set. To set a proxy url, set the PROXY_URL env variable (e.g. set PROXY_URL=proxy.com)");
}
//
// Http get request disabling redirects
//
cm.heading('get request disabling redirects');
httpc = new httpm.HttpClient('vsts-node-api', null, { allowRedirects: false });
res = await httpc.get("http://httpbin.org/redirect-to?url=" + encodeURIComponent("http://httpbin.org/get"))
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
// reset the client
httpc = new httpm.HttpClient('vsts-node-api');
//
// Http get request implicity allowing redirects
//
cm.heading('get request with implicitly allowed redirects');
res = await httpc.get("http://httpbin.org/redirect-to?url=" + encodeURIComponent("http://httpbin.org/get"))
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
//
// Http get piping to another stream
// response message is an IncomingMessage which is a stream
//
cm.heading('get request and pipe stream');
let file: NodeJS.WritableStream = fs.createWriteStream(sampleFilePath);
(await httpc.get('https://httpbin.org/get')).message.pipe(file);
body = fs.readFileSync(sampleFilePath).toString();
cm.outputHttpBinResponse(body);
// HEAD request
cm.heading('head request');
res = await httpc.head('https://httpbin.org/get');
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
// DELETE request
cm.heading('delete request');
res = await httpc.del('https://httpbin.org/delete');
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
let b: string = 'Hello World!';
// POST request
cm.heading('post request');
res = await httpc.post('https://httpbin.org/post', b);
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
// PATCH request
cm.heading('patch request');
res = await httpc.patch('https://httpbin.org/patch', b);
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
cm.heading('options request');
res = await httpc.options('https://httpbin.org');
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
// GET not found
cm.heading('get not found');
res = await httpc.get('https://httpbin.org/status/404');
body = await res.readBody();
cm.outputHttpBinResponse(body, res.message);
}
catch (err) {
console.error('Failed: ' + err.message);
}
}