Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Last active October 18, 2017 21:49
Show Gist options
  • Save shailrshah/1466b0d01be220891cf25443f49e9897 to your computer and use it in GitHub Desktop.
Save shailrshah/1466b0d01be220891cf25443f49e9897 to your computer and use it in GitHub Desktop.
GET an POST json objects
const root = "https://jsonplaceholder.typicode.com"
const getURL = "/posts/1";
const postURL = "/posts";
const getInput = () => {
return fetch(root + getURL)
.then((response) => {
console.log(response.status + " " + response.ok);
return response.json();
});
};
const doSomethingRandom = (jsonObj) => {
console.log(jsonObj);
jsonObj.newKey = "new Value";
jsonObj.userId += 100;
delete jsonObj.title;
console.log(jsonObj);
return jsonObj;
};
const doSomethingElse = (jsonObj) => {
jsonObj.completelyNewField = 10;
return jsonObj;
};
const postOutput = (jsonObj) => {
console.log(jsonObj);
const request = new Request(root + postURL, {
method: "post",
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: jsonObj
});
return fetch(request)
.then((response) => {
console.log(response.status + " " + response.ok);
return response.json();
});
};
const concludeCode = (jsonObj) => {
console.log(jsonObj);
console.log("End of program.");
return;
};
[getInput, doSomethingRandom, doSomethingElse, postOutput, concludeCode]
.reduce((a, b) => a.then(b), Promise.resolve());
// getInput()
// .then(response => doSomethingRandom(response))
// .then(response => postOutput(response))
// .then(response => concludeCode(response));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment