-
Notifications
You must be signed in to change notification settings - Fork 2
/
httpFunctions.js
49 lines (39 loc) · 1.19 KB
/
httpFunctions.js
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
/*
HTTP Client
Credits: https://gist.github.com/billybong/a462152889b6616deb02
*/
function httpGet(theUrl) {
var con = new java.net.URL(theUrl).openConnection();
con.requestMethod = "GET";
return asResponse(con);
}
function httpPost(theUrl, data, contentType) {
contentType = contentType || "application/json";
var con = new java.net.URL(theUrl).openConnection();
con.requestMethod = "POST";
con.setRequestProperty("Content-Type", contentType);
// Send post request
con.doOutput = true;
write(con.outputStream, data);
return asResponse(con);
}
function asResponse(con) {
var d = read(con.inputStream);
return { data: d, statusCode: con.responseCode };
}
function write(outputStream, data) {
var wr = new java.io.DataOutputStream(outputStream);
wr.writeBytes(data);
wr.flush();
wr.close();
}
function read(inputStream) {
var inReader = new java.io.BufferedReader(new java.io.InputStreamReader(inputStream));
var inputLine;
var response = new java.lang.StringBuffer();
while ((inputLine = inReader.readLine()) != null) {
response.append(inputLine);
}
inReader.close();
return response.toString();
}