Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ // DOM Elements
+ const sendButton = document.getElementById('sendRequest');
+ const urlInput = document.getElementById('url');
+ const methodSelect = document.getElementById('method');
+ const requestBody = document.getElementById('requestBody');
+ const headersContainer = document.getElementById('headers');
+ const addHeaderButton = document.getElementById('addHeader');
+ const responseContent = document.getElementById('responseContent');
+ const statusCode = document.getElementById('statusCode');
+ const statusText = document.getElementById('statusText');
+ const responseTab = document.getElementById('responseTab');
+ const headersTab = document.getElementById('headersTab');
+ const exampleButtons = document.querySelectorAll('.example-button');
+
+ // Add initial header row
+ addHeaderRow();
+
+ // Event Listeners
+ sendButton.addEventListener('click', sendRequest);
+ addHeaderButton.addEventListener('click', addHeaderRow);
+ responseTab.addEventListener('click', () => switchTab('response'));
+ headersTab.addEventListener('click', () => switchTab('headers'));
+
+ // Example buttons
+ exampleButtons.forEach(button => {
+ button.addEventListener('click', function() {
+ const url = this.getAttribute('data-url');
+ const method = this.getAttribute('data-method');
+ urlInput.value = url;
+ methodSelect.value = method;
+ });
+ });
+
+ // Functions
+ function addHeaderRow() {
+ const headerRow = document.createElement('div');
+ headerRow.className = 'header-row';
+
+ const keyInput = document.createElement('input');
+ keyInput.type = 'text';
+ keyInput.placeholder = 'Header key';
+
+ const valueInput = document.createElement('input');
+ valueInput.type = 'text';
+ valueInput.placeholder = 'Header value';
+
+ const removeButton = document.createElement('button');
+ removeButton.textContent = '×';
+ removeButton.className = 'remove-header';
+ removeButton.addEventListener('click', function() {
+ headersContainer.removeChild(headerRow);
+ });
+
+ headerRow.appendChild(keyInput);
+ headerRow.appendChild(valueInput);
+ headerRow.appendChild(removeButton);
+ headersContainer.appendChild(headerRow);
+ }
+
+ function getHeaders() {
+ const headers = {};
+ const headerRows = headersContainer.querySelectorAll('.header-row');
+
+ headerRows.forEach(row => {
+ const inputs = row.querySelectorAll('input');
+ const key = inputs[0].value.trim();
+ const value = inputs[1].value.trim();
+
+ if (key && value) {
+ headers[key] = value;
+ }
+ });
+
+ return headers;
+ }
+
+ async function sendRequest() {
+ const url = urlInput.value.trim();
+ const method = methodSelect.value;
+ const body = requestBody.value.trim();
+ const headers = getHeaders();
+
+ if (!url) {
+ alert('Please enter a URL');
+ return;
+ }
+
+ try {
+ sendButton.disabled = true;
+ sendButton.textContent = 'Sending...';
+
+ const options = {
+ method,
+ headers: {
+ 'Content-Type': 'application/json',
+ ...headers
+ }
+ };
+
+ if (method !== 'GET' && body) {
+ options.body = body;
+ }
+
+ const response = await fetch(url, options);
+
+ // Update status
+ statusCode.textContent = response.status;
+ statusCode.className = 'status-code ' +
+ (response.ok ? 'success' : 'error');
+ statusText.textContent = response.statusText;
+
+ // Get response data
+ let responseData;
+ try {
+ responseData = await response.json();
+ responseContent.textContent = JSON.stringify(responseData, null, 2);
+ } catch (e) {
+ responseData = await response.text();
+ responseContent.textContent = responseData;
+ }
+
+ // Show response tab by default
+ switchTab('response');
+
+ } catch (error) {
+ statusCode.textContent = 'Error';
+ statusCode.className = 'status-code error';
+ statusText.textContent = '';
+ responseContent.textContent = error.message;
+ } finally {
+ sendButton.disabled = false;
+ sendButton.textContent = 'Send Request';
+ }
+ }
+
+ function switchTab(tab) {
+ if (tab === 'response') {
+ responseTab.classList.add('tab-active');
+ headersTab.classList.remove('tab-active');
+ // In a real app, you would show response content here
+ } else {
+ headersTab.classList.add('tab-active');
+ responseTab.classList.remove('tab-active');
+ // In a real app, you would show headers content here
+ }
+ }
+ });