Created
August 12, 2014 14:29
-
-
Save Ugmaxie/38e4b8ffd12b056dd2bf to your computer and use it in GitHub Desktop.
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
var http = require('http'); | |
var querystring = require('querystring'); | |
var post_data = querystring.stringify({ | |
username : 'demo', | |
password: 'demo' | |
}); | |
var options = { | |
hostname: 'tableready.herokuapp.com', | |
port: 80, | |
path: '/login', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': post_data.length | |
} | |
}; | |
var cook; | |
var req = http.request(options, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
cook = res.headers['set-cookie']; | |
return onSuccessLogin(cook); | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
req.end(post_data); | |
function onSuccessLogin(cook){ | |
var post_sms = querystring.stringify({ | |
message: "Your table's ready at Roberts Roadhouse. Please see the host to be seated. test1", | |
phone: "1231231231", | |
time: Date.now() | |
}); | |
console.log(cook); | |
var h = {}; | |
h['content-type'] = 'application/x-www-form-urlencoded'; | |
h['content-length'] = post_sms.length; | |
h['cookie'] = cook[0]; | |
var o = options; | |
o.path = '/v.1/smsPage'; | |
o.headers = h; | |
var req_sms = http.request(o, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('BODY: ' + chunk); | |
}); | |
}); | |
req_sms.on('error', function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
console.log(h); | |
req_sms.end(post_sms); | |
} | |
return; |
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
var request = require('request'); | |
var login = 'http://tableready.herokuapp.com/login'; | |
var sms = 'http://tableready.herokuapp.com/v.1/smsPage'; | |
var cook; | |
var req_login = request.post(login, function (err, httpResponse, body) { | |
if (err) { | |
return console.error('npm request err:', err); | |
} | |
cook = httpResponse.headers['set-cookie']; | |
onSuccessLogin(cook); | |
}); | |
var form1 = req_login.form(); | |
form1.append('username', 'demo'); | |
form1.append('password', 'demo'); | |
function onSuccessLogin(cook) { | |
var querystring = require('querystring'); | |
var form = { | |
message: "Your table's ready at Roberts Roadhouse. Please see the host to be seated. test1", | |
phone: "1231231231", | |
time: Date.now() | |
}; | |
var formLength = querystring.stringify(form).length; | |
var options = { | |
url: sms, | |
method: 'post', | |
form: form, | |
headers: { | |
'content-type': 'application/x-www-form-urlencoded', | |
'content-length': formLength, | |
'cookie': cook[0], | |
'time': Date.now() | |
} | |
}; | |
function bibi (error, response, body) { | |
if (error) { | |
console.log(error); | |
} else { | |
console.log(body); | |
} | |
} | |
request (options, bibi); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1. 'node' and 'npm' should be installed:
2. Save 'app-http.js' to file run from terminal:
3. Save app-request.js to file run from termianl: