Changed around line 1
+ document.addEventListener('DOMContentLoaded', function() {
+ // DOM elements
+ const sendButton = document.querySelector('.send-button');
+ const methodSelect = document.getElementById('method');
+ const endpointInput = document.getElementById('endpoint');
+ const requestBody = document.getElementById('request-body');
+ const statusCode = document.getElementById('status-code');
+ const responseHeaders = document.getElementById('response-headers');
+ const responseBody = document.getElementById('response-body');
+ const headersContainer = document.querySelector('.headers-container');
+ const addHeaderButton = document.querySelector('.add-header');
+ const exampleButtons = document.querySelectorAll('.example-button');
+
+ // Add header row
+ function addHeaderRow(key = '', value = '') {
+ const headerRow = document.createElement('div');
+ headerRow.className = 'header-row';
+ headerRow.innerHTML = `
+
+
+
+ `;
+ headersContainer.appendChild(headerRow);
+
+ // Add remove header functionality
+ headerRow.querySelector('.remove-header').addEventListener('click', function() {
+ headerRow.remove();
+ });
+ }
+
+ // Initialize with one empty header row
+ addHeaderRow();
+
+ // Add header button click handler
+ addHeaderButton.addEventListener('click', function() {
+ addHeaderRow();
+ });
+
+ // Example button click handlers
+ exampleButtons.forEach(button => {
+ button.addEventListener('click', function() {
+ endpointInput.value = button.dataset.endpoint;
+ methodSelect.value = 'GET';
+ requestBody.value = '';
+
+ // Clear headers and add one empty
+ headersContainer.innerHTML = '';
+ addHeaderRow();
+ });
+ });
+
+ // Send request
+ sendButton.addEventListener('click', async function() {
+ const method = methodSelect.value;
+ const endpoint = endpointInput.value.trim();
+
+ if (!endpoint) {
+ alert('Please enter an endpoint URL');
+ return;
+ }
+
+ // Collect headers
+ const headers = {};
+ const headerRows = document.querySelectorAll('.header-row');
+ headerRows.forEach(row => {
+ const key = row.querySelector('.header-key').value.trim();
+ const value = row.querySelector('.header-value').value.trim();
+ if (key && value) {
+ headers[key] = value;
+ }
+ });
+
+ // Prepare request options
+ const options = {
+ method: method,
+ headers: headers
+ };
+
+ // Add body for methods that typically have one
+ if (['POST', 'PUT', 'PATCH'].includes(method) && requestBody.value.trim()) {
+ try {
+ JSON.parse(requestBody.value); // Validate JSON
+ options.body = requestBody.value;
+ if (!headers['Content-Type']) {
+ options.headers['Content-Type'] = 'application/json';
+ }
+ } catch (e) {
+ alert('Invalid JSON in request body');
+ return;
+ }
+ }
+
+ try {
+ sendButton.disabled = true;
+ sendButton.textContent = 'Sending...';
+
+ const response = await fetch(endpoint, options);
+
+ // Update status
+ statusCode.textContent = `${response.status} ${response.statusText}`;
+ statusCode.style.color = response.ok ? 'green' : 'red';
+
+ // Update headers
+ const headersObj = {};
+ response.headers.forEach((value, key) => {
+ headersObj[key] = value;
+ });
+ responseHeaders.textContent = JSON.stringify(headersObj, null, 2);
+
+ // Update body
+ try {
+ const data = await response.json();
+ responseBody.textContent = JSON.stringify(data, null, 2);
+ } catch {
+ const text = await response.text();
+ responseBody.textContent = text;
+ }
+ } catch (error) {
+ statusCode.textContent = 'Error';
+ statusCode.style.color = 'red';
+ responseHeaders.textContent = '';
+ responseBody.textContent = error.message;
+ } finally {
+ sendButton.disabled = false;
+ sendButton.textContent = 'Send Request';
+ }
+ });
+
+ // Pretty print JSON when pasted into request body
+ requestBody.addEventListener('paste', function(e) {
+ setTimeout(() => {
+ try {
+ const json = JSON.parse(requestBody.value);
+ requestBody.value = JSON.stringify(json, null, 2);
+ } catch (e) {
+ // Not JSON, leave as is
+ }
+ }, 10);
+ });
+ });