Below is a sample JavaScript code that sends a POST request to an API to shorten a long URL:
const apiKey = "YOUR_API_KEY"; // Replace with your actual API key
const data = {
originalUrl: "https://www.example.com/very/long/url", // Example URL to shorten
apiKey: apiKey, // Include the API key here
};
fetch("/api/shorten", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
})
.then((response) => {
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then((data) => {
console.log("Success:", data);
})
.catch((error) => {
console.error("Error:", error);
});
YOUR_API_KEY
with
your actual API key.