Skip to content

Instantly share code, notes, and snippets.

@Lahirutech
Created March 21, 2024 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lahirutech/8133c20ccf3b9b5ecabff282babae66f to your computer and use it in GitHub Desktop.
Save Lahirutech/8133c20ccf3b9b5ecabff282babae66f to your computer and use it in GitHub Desktop.
import { useState } from 'react'
import './App.css'
export default function App() {
const [url, setUrl] = useState("")
const [shurl, setShUrl] = useState("")
// https://api-ssl.bitly.com/v4/shorten
const postUrl = () => {
fetch("https://api-ssl.bitly.com/v4/shorten", {
method: "post",
headers: {
"Content-Type": "application/json",
"Authorization": 'Bearer ' + import.meta.env.VITE_BITLY
}, body: JSON.stringify({ long_url: url })
}).then(res => res.json()).then(data => {
console.log(data)
setShUrl(data.link)
})
}
const handleSubmit = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
console.log("called")
postUrl()
}
return (
<main>
SHort URL:{shurl} <br />
Input Url: <input type="text" value={url} onChange={(e) => setUrl(e.target.value)} /> <br />
<button onClick={(e) => handleSubmit(e)}>Generate Short URL</button>
</main>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment