HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html lang="en">
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, shrink-to-fit=no">
<title>Hoppscotch</title>
<meta name="description" content="Hoppscotch">
<link rel="icon" href="favicon.ico">
<meta itemprop="name" content="Hoppscotch">
<meta itemprop="description" content="Hoppscotch">
<meta itemprop="image" content="icons/icon-192x192.png">
<!-- See https://goo.gl/OOhYW5 -->
<link rel="manifest" href="manifest.json">
<!-- See https://goo.gl/qRE0vM -->
<meta name="theme-color" content="#121212">
<!-- Add to homescreen for Chrome on Android. Fallback for manifest.json -->
<meta name="mobile-web-app-capable" content="yes">
<meta name="application-name" content="Hoppscotch">
<!-- Add to homescreen for Safari on iOS -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="apple-mobile-web-app-title" content="Hoppscotch">
<!-- Homescreen icons -->
<link rel="apple-touch-icon" href="icons/icon-48x48.png">
<link rel="apple-touch-icon" sizes="72x72" href="icons/icon-72x72.png">
<link rel="apple-touch-icon" sizes="96x96" href="icons/icon-96x96.png">
<link rel="apple-touch-icon" sizes="144x144" href="icons/icon-144x144.png">
<link rel="apple-touch-icon" sizes="192x192" href="icons/icon-192x192.png">
<!-- Tile icon for Windows 8 (144x144 + tile color) -->
<meta name="msapplication-TileImage" content="icons/icon-144x144.png">
<meta name="msapplication-TileColor" content="#121212">
<meta name="msapplication-tap-highlight" content="no">
<!-- OpenGraph -->
<meta property="og:site_name" content="Hoppscotch">
<meta property="og:url" content="https://hoppscotch.io">
<meta property="og:type" content="website">
<meta property="og:title" content="Hoppscotch">
<meta property="og:description" content="API request builder">
<meta property="og:image" content="icons/icon-144x144.png">
<!-- Twitter -->
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@liyasthomas">
<meta name="twitter:creator" content="@liyasthomas">
<meta name="twitter:url" content="https://hoppscotch.io">
<meta name="twitter:title" content="Postwoman">
<meta name="twitter:description" content="API request builder">
<meta name="twitter:image" content="icons/icon-144x144.png">
<!-- Web Fonts -->
<link href="https://fonts.googleapis.com/css?family=Poppins:500,700&display=swap" rel="stylesheet">
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('sw.js').then(function(registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
console.log('ServiceWorker registration failed: ', err);
});
});
}
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js'></script>
</head>
<body>
<main id="app">
<header>
<div>
<a href="https://github.com/hoppscotch/hoppscotch">
<h1 class="logo">Hoppscotch</h1>
</a>
<h3>API testing tool</h3>
</div>
</header>
<fieldset class="request">
<legend>Request</legend>
<ul>
<li>
<label for="method">Method</label>
<select v-model="method">
<option>GET</option>
<option>POST</option>
<option>PUT</option>
<option>DELETE</option>
<option>OPTIONS</option>
</select>
</li>
<li>
<label for="url">URL</label>
<input v-model="url">
</li>
<li>
<label for="path">Path</label>
<input v-model="path">
</li>
<li>
<label for="action">Action</label>
<button name="action" @click="sendRequest">Send</button>
</li>
</ul>
</fieldset>
<fieldset class="authentication">
<legend>Authentication</legend>
<ul>
<li>
<label for="auth">Authentication Type</label>
<select v-model="auth">
<option>None</option>
<option>Basic</option>
</select>
</li>
</ul>
<ul v-if="auth === 'Basic'">
<li>
<label for="http_basic_user">User</label>
<input v-model="httpUser">
</li>
<li>
<label for="http_basic_passwd">Password</label>
<input v-model="httpPassword" type="password">
</li>
</ul>
</fieldset>
<fieldset class="3">
<legend>Parameters</legend>
<ol v-for="(param, index) in params">
<li>
<label :for="'param'+index">Key {{index + 1}}</label>
<input :name="'param'+index" v-model="param.key">
</li>
<li>
<label :for="'value'+index">Value {{index + 1}}</label>
<input :name="'value'+index" v-model="param.value">
</li>
<li>
<label for="param">Action</label>
<button name="param" @click="removeRequestParam(index)">Remove</button>
</li>
</ol>
<ul>
<li>
<label for="add">Action</label>
<button name="add" @click="addRequestParam">Add</button>
</li>
</ul>
<ul>
<li>
<label for="request">Parameter List</label>
<textarea name="request" rows="1" readonly>{{queryString || '(add atleast one parameter)'}}</textarea>
</li>
</ul>
</fieldset>
<fieldset class="4" v-if="method === 'POST' || method === 'PUT'">
<legend>Request Body</legend>
<ol v-for="(param, index) in bodyParams">
<li>
<label :for="'bparam'+index">Key {{index + 1}}</label>
<input :name="'bparam'+index" v-model="param.key">
</li>
<li>
<label :for="'bvalue'+index">Value {{index + 1}}</label>
<input :name="'bvalue'+index" v-model="param.value">
</li>
<li>
<label for="request">Action</label>
<button name="request" @click="removeRequestBodyParam(index)">Remove</button>
</li>
</ol>
<ul>
<li>
<label>Content Type</label>
<select v-model="contentType">
<option>application/json</option>
<option>www-form/urlencoded</option>
</select>
</li>
<li>
<label for="addrequest">Action</label>
<button name="addrequest" @click="addRequestBodyParam">Add</button>
</li>
</ul>
<ul>
<li>
<label for="request">Parameter List</label>
<textarea name="request" rows="1" readonly>{{rawRequestBody || '(add atleast one parameter)'}}</textarea>
</li>
</ul>
</fieldset>
<fieldset class="5 response" id="response" ref="response">
<legend>Response</legend>
<ul>
<li>
<label for="status">Status</label>
<input name="status" type="text" readonly :value="response.status || 'waiting for request'">
</li>
</ul>
<ul v-for="(value, key) in response.headers">
<li>
<label for="value">{{key}}</label>
<input name="value" :value="value" readonly>
</li>
</ul>
<textarea rows="10" readonly>{{response.body}}</textarea>
</fieldset>
<footer>
<a href="https://github.com/hoppscotch/hoppscotch"><img src="icons/github.svg" alt="" style="margin-right: 16px">GitHub</a>
<button id="installPWA" onclick="installPWA()">
PWA
</button>
</footer>
</main>
<script src="script.js"></script>
</body>
</html>
let pwaInstalled = localStorage.getItem("pwaInstalled") == "yes";
if (window.matchMedia("(display-mode: standalone)").matches) {
localStorage.setItem("pwaInstalled", "yes");
pwaInstalled = true;
}
if (window.navigator.standalone === true) {
localStorage.setItem("pwaInstalled", "yes");
pwaInstalled = true;
}
if (pwaInstalled) {
document.getElementById("installPWA").style.display = "none";
} else {
document.getElementById("installPWA").style.display = "block";
}
let deferredPrompt = null;
window.addEventListener("beforeinstallprompt", (e) => {
deferredPrompt = e;
});
async function installPWA() {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then(({ outcome }) => {
if (outcome === "accepted") {
console.log("Your PWA has been installed");
} else {
console.log("User chose to not install your PWA");
}
deferredPrompt = null;
});
}
}
window.addEventListener("appinstalled", (evt) => {
localStorage.setItem("pwaInstalled", "yes");
pwaInstalled = true;
document.getElementById("installPWA").style.display = "none";
});
const parseHeaders = (xhr) => {
const headers = xhr
.getAllResponseHeaders()
.trim()
.split(/[\r\n]+/);
const headerMap = {};
headers.forEach((line) => {
const parts = line.split(": ");
const header = parts.shift().toLowerCase();
const value = parts.join(": ");
headerMap[header] = value;
});
return headerMap;
};
const app = new Vue({
el: "#app",
data: {
method: "GET",
url: "https://yesno.wtf",
auth: "None",
path: "/api",
httpUser: "",
httpPassword: "",
params: [],
bodyParams: [],
contentType: "application/json",
response: {
status: "",
headers: "",
body: ""
}
},
computed: {
rawRequestBody() {
const { bodyParams } = this;
if (this.contentType === "application/json") {
try {
const obj = JSON.parse(
`{${bodyParams
.filter(({ key }) => !!key)
.map(
({ key, value }) => `
"${key}": "${value}"
`
)
.join()}}`
);
return JSON.stringify(obj);
} catch (ex) {
return "invalid";
}
} else {
return bodyParams
.filter(({ key }) => !!key)
.map(({ key, value }) => `${key}=${encodeURIComponent(value)}`)
.join("&");
}
},
queryString() {
const result = this.params
.filter(({ key }) => !!key)
.map(({ key, value }) => `${key}=${encodeURIComponent(value)}`)
.join("&");
return result == "" ? "" : `?${result}`;
}
},
methods: {
sendRequest() {
this.$refs.response.scrollIntoView({
behavior: "smooth"
});
this.response.status = "Loading...";
const xhr = new XMLHttpRequest();
const user = this.auth === "Basic" ? this.httpUser : null;
const pswd = this.auth === "Basic" ? this.httpPassword : null;
xhr.open(
this.method,
this.url + this.path + this.queryString,
true,
user,
pswd
);
if (this.method === "POST" || this.method === "PUT") {
const requestBody = this.rawRequestBody;
xhr.setRequestHeader("Content-Length", requestBody.length);
xhr.setRequestHeader(
"Content-Type",
`${this.contentType}; charset=utf-8`
);
xhr.send(requestBody);
} else {
xhr.send();
}
xhr.onload = (e) => {
this.response.status = xhr.status;
const headers = (this.response.headers = parseHeaders(xhr));
if ((headers["content-type"] || "").startsWith("application/json")) {
this.response.body = JSON.stringify(
JSON.parse(xhr.responseText),
null,
2
);
} else {
this.response.body = xhr.responseText;
}
};
},
addRequestParam() {
this.params.push({
key: "",
value: ""
});
return false;
},
removeRequestParam(index) {
this.params.splice(index, 1);
},
addRequestBodyParam() {
this.bodyParams.push({
key: "",
value: ""
});
return false;
},
removeRequestBodyParam(index) {
this.bodyParams.splice(index, 1);
}
}
});
Also see: Tab Triggers