-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (77 loc) · 2.99 KB
/
Copy pathscript.js
File metadata and controls
94 lines (77 loc) · 2.99 KB
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
let parameterBox = document.getElementById("parameterBox")
let requestJsonBox = document.getElementById("requestJsonBox")
let params= document.getElementById('params')
parameterBox.style.display = 'none'
let paramsRadio = document.getElementById('paramsradio')
let jsonRadio = document.getElementById('jsonradio')
let addedParamsCount = 1
paramsRadio.addEventListener("click",()=>{
requestJsonBox.style.display = 'none'
parameterBox.style.display = 'block'
params.style.display = 'block'
})
jsonRadio.addEventListener("click",()=>{
parameterBox.style.display = 'none'
requestJsonBox.style.display = 'block'
params.style.display = 'none'
})
function getElementFromString(string){
let div = document.createElement('div') //<div></div>
div.innerHTML = string;// <div>...</div>
return div.firstElementChild
}
let addParam = document.getElementById('addParam')
addParam.addEventListener("click",function(){
addedParamsCount++;
let string = `
<div class="form-row my-2">
<label for="url" class="col-sm-2 col-form-label">Parameter ${addedParamsCount}</label>
<div class="col-md-4">
<input type="text" class="form-control" id="parameterkey${addedParamsCount}" placeholder="Enter Parameter ${addedParamsCount} Key">
</div>
<div class="col-md-4">
<input type="text" class="form-control" id="parametervalue${addedParamsCount}" placeholder="Enter Parameter ${addedParamsCount} Value">
</div>
<button type="button" id="addParam" class="btn btn-primary deleteParam">-</button>
</div>
`
params.appendChild(getElementFromString(string))
})
let submit = document.getElementById("submit")
submit.addEventListener("click",()=>{
document.getElementById('responseJsonText').value = 'Please Wait....'
let requestType = document.querySelector("input[name=requestType]:checked").value
let contentType = document.querySelector("input[name=contentType]:checked").value
let url = document.getElementById('urlField').value
let data = {}
if(contentType === 'params'){
for(let i=1;i<=addedParamsCount;i++){
let key = document.getElementById('parameterkey'+i).value;
let value = document.getElementById('parametervalue'+i).value
data[key] = value // {name:"david"}
}
console.log(data)
}else{
data = document.getElementById('requestJsonText').value
}
console.log(data)
if(requestType === 'POST'){
fetch(url,{
method:"POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(res => res.json())
.then(res => {
document.getElementById('responseJsonText').value = JSON.stringify(res)
})
}else{
fetch(url,{method:"GET"})
.then(res => res.json())
.then(res => {
document.getElementById('responseJsonText').value = JSON.stringify(res)
})
}
})