Last active
January 26, 2018 17:07
-
-
Save adrianseeley/98391a33fdcf4e828713670060c3bfa8 to your computer and use it in GitHub Desktop.
Async Fetch JSON POST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Getting cors errors using lambda? make sure your lambda response as well as your api gateway has cors established | |
(in your lambda response) | |
headers: { | |
'Content-Type': 'application/json', | |
'Access-Control-Allow-Origin': '*' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const post = async (url, body, cb) => { | |
const opts = { | |
headers: { | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
method: "POST", | |
body: JSON.stringify(body) | |
}; | |
try { | |
const response = await fetch(url, opts); | |
const responseBody = await response.json(); | |
return cb(null, responseBody); | |
} catch (err) { | |
return cb(err); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment