diff --git a/2-copy-of-code/lesson-18/lesson18.html b/2-copy-of-code/lesson-18/lesson18.html index b60a730..368e57f 100644 --- a/2-copy-of-code/lesson-18/lesson18.html +++ b/2-copy-of-code/lesson-18/lesson18.html @@ -49,6 +49,7 @@ postGreeting(); */ + /* async function getAmazon() { try { const response = await fetch('https://amazon.com'); @@ -60,6 +61,34 @@ } } getAmazon(); + */ + + async function postGreeting() { + try { + const response = await fetch('https://supersimplebackend.dev/greeting', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + } + }); + + if (response.status >= 400) { + throw response; + } + + const text = await response.text(); + console.log(text); + + } catch (error) { + if (error.status === 400) { + const errorMessage = await error.json(); + console.log(errorMessage); + } else { + console.log('Network error. Please try again later.'); + } + } + } + postGreeting(); \ No newline at end of file