Last active
October 18, 2017 21:49
-
-
Save shailrshah/1466b0d01be220891cf25443f49e9897 to your computer and use it in GitHub Desktop.
GET an POST json objects
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 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