Skip to content

Instantly share code, notes, and snippets.

@ahmedghazi
Created February 18, 2020 10:43
Show Gist options
  • Save ahmedghazi/c87170bb5d622df36d5831bf69eafe50 to your computer and use it in GitHub Desktop.
Save ahmedghazi/c87170bb5d622df36d5831bf69eafe50 to your computer and use it in GitHub Desktop.
import axios from "axios"
export async function handler(event, context) {
try {
const response = await axios.get("https://icanhazdadjoke.com", { headers: { Accept: "application/json" } })
const data = response.data
return {
statusCode: 200,
body: JSON.stringify({ msg: data.joke })
}
} catch (err) {
console.log(err) // output to netlify function log
return {
statusCode: 500,
body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment