Last active
March 23, 2016 15:11
-
-
Save vik-y/f8c899bd4cc263c0af49 to your computer and use it in GitHub Desktop.
Node js. Doing a get and post request, parsing a json
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
/* | |
This code might be useful for writing get and post requests and rendering json files when you are using node js | |
*/ | |
var request = require('request'); | |
base_url = "" | |
function getComplaints(wardName, callback){ | |
request(base_url+'getComplaints.json?name='+wardName, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
//console.log(body); // Print the google web page. | |
var obj = JSON.parse(body); | |
//console.log(body.id) | |
//console.log(obj) | |
//console.log(obj.length); | |
//console.log(obj); | |
if (obj.length == 0){ | |
console.log("No complaints found"); | |
} | |
else { | |
var output = "" | |
//console.log("Check"); | |
for(var i=0; i < obj.length; i++){ | |
var Cid = "Complaint id: " + obj[i].id; | |
//var status = "Status: " + obj[i].status; | |
var title = "Title: " + obj[i].title; | |
var description = "Description: " + obj[i].description; | |
//var cost = "Estimated Cost: Rs " + obj[i].estimated; | |
output += Cid + "<br>" + title + "<br>" + description + "<br><br>"; | |
} | |
} | |
callback(output) | |
//console.log(obj[0].id); | |
} | |
}); | |
} | |
function uploadFeedback(tender_serial, feedback, callback){ | |
var url = base_url+'/comments.json' | |
var options = { | |
method: 'POST', | |
body: "comment[body]=" + feedback + "&comment[tender_serial]=" + tender_serial, | |
url: url | |
} | |
request.post(options, function (err, res, body) { | |
if (err) { | |
console.log('error posting json') | |
return | |
} | |
var headers = res.headers | |
var statusCode = res.statusCode | |
console.log("body: " + body) | |
j = JSON.parse(body) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment