Skip to content

Instantly share code, notes, and snippets.

@TobyEalden
Last active December 20, 2015 20:39
Show Gist options
  • Save TobyEalden/6192116 to your computer and use it in GitHub Desktop.
Save TobyEalden/6192116 to your computer and use it in GitHub Desktop.
POST to API implemented using express
var express = require("express");
var app = express();
app.use(express.bodyParser());
app.post('/postTest', function(req, res) {
if (req.body.hasOwnProperty("test")) {
console.log("test property is: " + req.body.test);
res.json({ok: true });
} else {
console.log("no test property");
res.json({ok: false });
}
});
app.listen(5000);
var restClient = require("node-rest-client").Client;
var appgAPIClient = new restClient();
var apiURLBase = "http://localhost:5000";
var args = {
data: { test: "hello" },
headers: {}
};
appgAPIClient.post(apiURLBase + "/postTest", args, function(data,response) {
if (data.hasOwnProperty("ok") && data.ok === true) {
console.log("success");
} else {
console.log("failure");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment