import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [url, setUrl] = useState('https://jsonplaceholder.typicode.com/posts/1');
const [method, setMethod] = useState('GET');
const [headers, setHeaders] = useState('');
const [body, setBody] = useState('');
const [response, setResponse] = useState('');
const handleSubmit = async (e) => {
e.preventDefault();
try {
const options = {
method,
headers: headers ? JSON.parse(headers) : {},
body: method !== 'GET' && body ? body : undefined
};
const res = await fetch('/proxy', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url, options })
});
const data = await res.json();
setResponse(JSON.stringify(data, null, 2));
} catch (error) {
setResponse(`Error: ${error.message}`);
}
};
return (
<div className="container">
<h1>🚀 Browser Postman Clone</h1>
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="url">URL:</label>
<input
type="text"
id="url"
value={url}
onChange={(e) => setUrl(e.target.value)}
required
/>
</div>
<div>
<label htmlFor="method">Method:</label>
<select
id="method"
value={method}
onChange={(e) => setMethod(e.target.value)}
>
<option value="GET">GET</option>
<option value="POST">POST</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div>
<label htmlFor="headers">Headers (JSON):</label>
<textarea
id="headers"
value={headers}
onChange={(e) => setHeaders(e.target.value)}
placeholder='{"Content-Type": "application/json"}'
/>
</div>
<div>
<label htmlFor="body">Body:</label>
<textarea
id="body"
value={body}
onChange={(e) => setBody(e.target.value)}
placeholder="Request body (if applicable)"
/>
</div>
<button type="submit">Send Request</button>
</form>
<div className="response">
<h2>Response:</h2>
<pre>{response}</pre>
</div>
<footer>
<a href={import.meta.url.replace("esm.town", "val.town")} target="_blank">View Source</a>
</footer>
</div>
);
}
function client() {
createRoot(document.getElementById("root")).render(<App />);
}
if (typeof document !== "undefined") {