Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Last active January 26, 2018 17:07
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 adrianseeley/98391a33fdcf4e828713670060c3bfa8 to your computer and use it in GitHub Desktop.
Save adrianseeley/98391a33fdcf4e828713670060c3bfa8 to your computer and use it in GitHub Desktop.
Async Fetch JSON POST
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': '*'
}
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