-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_tester.js
111 lines (106 loc) · 3.39 KB
/
api_tester.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
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
var form;
var selMethod;
var txtRequest;
var txtAction;
var txtCHeader;
var txtCHeaderData;
var selCHeaders;
var selRequestHeader;
var txtResponseHeader;
var btnSubmit;
var btnAddHeader;
var btnRemoveHeader;
var cHeaders=new Array();
var cDatas=new Array();
//Get Data
function sendRequest(resultObjID, resultCodeObjID, responseHeaderID, method, request, url, headers, datas, requestHeader)
{
var XMLHTTP= XMLHttpRequest || ActiveXObject("Microsoft.XMLHTTP");
if (typeof XMLHTTP!= "undefined" )
{
var xmlhttp = new XMLHTTP;
xmlhttp.onreadystatechange= function() {
if(xmlhttp.readyState== 4) //4 is recv'd all responses
{
var statusCode = xmlhttp.status;
var statusText = xmlhttp.statusText;
var resp = xmlhttp.responseText;
document.getElementById(resultCodeObjID).value = statusCode + " : " + statusText;
document.getElementById(responseHeaderID).innerHTML = xmlhttp.getAllResponseHeaders();
document.getElementById(resultObjID).innerHTML= resp;
}
}
xmlhttp.ontimeout= function() {
document.getElementById(resultCodeObjID).value = "A timeout has occurred.";
}
xmlhttp.open(method, url , true);
for (var i=0; i<headers.length; i++) {
if (datas[i]=""){
xmlhttp.setRequestHeader("Content-type", headers[i]);
} else {
xmlhttp.setRequestHeader(headers[i], datas[i]);
}
}
if (method != "GET") {
xmlhttp.setRequestHeader("Content-type", requestHeader);
xmlhttp.send(request);
} else {
xmlhttp.send(null);
}
}
else
alert("Your browser doesn't seem to support ajax");
}
function sendIt() {
var resultObjID = "results";
var resultCodeObjID = "resultCode";
var responseHeaderID = "resultHeader";
var method = selMethod.value;
var action = txtAction.value;
var request = txtRequest.value;
var requestHeader = selRequestHeader.value;
sendRequest(resultObjID, resultCodeObjID, responseHeaderID, method, request, action, cHeaders, cDatas, requestHeader);
}
function addHeader() {
if (txtCHeader.value=="") {
return;
} else {
var optNew = document.createElement("option");
optNew.text = txtCHeader.value;
selCHeaders.add(optNew);
cHeaders.push(txtCHeader.value);
cDatas.push(txtCHeaderData.value);
}
}
function removeHeader() {
var id = selCHeaders.selectedIndex;
cHeaders.splice(id, 1);
cDatas.splice(id, 1);
selCHeaders.remove(id);
}
window.onload = function() {
form = document.getElementById("testForm");
selMethod = document.getElementById("methodReq");
txtRequest = document.getElementById("request");
selRequestHeader = document.getElementById("requestHeaderType");
txtAction = document.getElementById("actiontext");
txtCHeader = document.getElementById("CHeaderName");
txtCHeaderData = document.getElementById("CHeaderData");
selCHeaders = document.getElementById("CHeaders");
txtResponseHeader = document.getElementById("resultHeader");
btnSubmit = document.getElementById("submit");
btnAddHeader = document.getElementById("btnAddHeader");
btnRemoveHeader = document.getElementById("btnRemoveHeader");
document.getElementById("requestFormArea").style.display ="none";
selMethod.addEventListener("change", function(){
switch (selMethod.value) {
case "GET":
case "DELETE":
document.getElementById("requestFormArea").style.display="none";
break;
default:
document.getElementById("requestFormArea").style.display="block";
break;
}
});
}