Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
/**
* This val creates a Postman-like interface for testing HTTP requests directly in the browser.
* It uses React for the UI and the Fetch API to make requests.
* The server function serves the HTML and handles the API requests.
*/
/** @jsxImportSource https://esm.sh/react */
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") {
maxm-postmanclone.web.val.run
August 24, 2024