-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
165 lines (148 loc) · 4.39 KB
/
index.html
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!doctype html>
<html>
<head>
<title>API tester</title>
<style>
html, body, select, option, input, textarea, button {
font: 300 14px/16px "Source Code Pro", monospace;
margin: 0; padding: 0; border: 0;
}
body {
width: 800px; margin: 20px auto; padding: 8px;
background: #eee; color: #000;
}
header { font-size: 28px; font-weight: 100; text-align: center; padding: 20px 0; }
p { padding: 0; margin: 4px 0; clear: both; }
select { padding: 6px 0; }
select, option, input, textarea, button {
background: #fff; color: #000; border: none;
height: 32px; vertical-align: top; z-index: 1;
}
select:focus, option:focus, input:focus, textarea:focus, button:focus {
outline: none; box-shadow: 0 3px 3px -3px #00f;
}
*[disabled] { color: #999; }
button {
font-weight: 700; background: #000; color: #fff;
}
textarea {
height: 288px; resize: vertical; padding: 8px 0;
-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4;
-ms-tab-size: 4; tab-size:4;
}
.w3 { width: 25%; }
.w6 { width: 50%; }
.w9 { width: 75%; }
.w12 { width: 100%; }
.fl { float: left; }
.fr { float: right; }
.nl { margin-right: 2px; margin-left: -2px; }
.nr { margin-left: 2px; margin-right: -2px; }
.tall { height: 324px; }
.error { background: #f66; }
</style>
</head>
<body>
<header>HTTP API Tester</header>
<form class="fl w6 nl">
<p>Request</p>
<p>
<select id="method" class="w3">
<option value="get">GET</option>
<option value="put">PUT</option>
<option value="post">POST</option>
<option value="delete">DELETE</option>
</select><input id="url" value="/" class="w9"/>
</p>
<p><textarea id="data" class="w12"></textarea></p>
<p><button id="submit" type="submit" class="w3 fr">Send</button>
HTTP headers (optional) and POST data separated by a blank line. Hit
enter thrice to submit.</p>
</form>
<div class="fr w6 nr">
<p>Response:</p>
<p><textarea id="response" readonly class="w12 tall"></textarea></p>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("form").submit(function(evt) {
var data = $("#data").val().split("\n\n"),
headers = "", postdata;
if(data.length > 1) {
postdata = data[1]; headers = data[0];
}
else {
if($("#method").val() == "post") postdata = data[0];
else headers = data[0];
}
$("#data").removeClass("error");
if(postdata) {
try { postdata = JSON.parse(postdata); }
catch(e) {
console.log("POSTDATA parse error:", e);
postdata = {};
$("#data").addClass("error");
}
}
if(headers) {
try { headers = JSON.parse(headers); }
catch(e) {
console.log("POSTDATA parse error:", e);
headers = {};
$("#data").addClass("error");
}
} else { headers = {}; }
if(!headers["Content-Type"])
headers["Content-Type"] = "application/json";
console.log(headers);
$.ajax({
url: $("#url").val(),
type: $("#method").val(),
data: JSON.stringify(postdata),
processData: false,
headers: headers,
dataType: "text",
success: function(data) {
try {
data = JSON.stringify(JSON.parse(data), null, 2);
$("#response").removeClass('error');
} catch(e) {
data = e + '\n' + data;
$("#response").addClass('error');
}
$("#response").val(data);
},
error: function(xhr, txt, err) {
$("#response").val(txt + "\n" + err).addClass('error');
}
});
evt.preventDefault();
return false;
});
$("#data").keydown(function(evt) {
var start = this.selectionStart,
end = this.selectionEnd,
pre = this.value.substring(0, start),
post = this.value.substring(end),
repl = false;
if(evt.which == 9) {
repl = "\t";
}
if(evt.which == 13) {
if(/\n\n$/.test(pre)) {
this.value = pre.substring(0, start-2);
$("#submit").focus(); return;
}
repl = '\n' + (/[\s\S]*(^|\n)([\t ]*).*$/m.exec(pre)[2]);
}
if(repl !== false) {
this.value = pre + repl + post;
this.selectionStart = this.selectionEnd = start + repl.length;
evt.preventDefault();
}
});
});
</script>
</body>
</html>