Created
February 9, 2012 20:35
-
-
Save mandric/1782893 to your computer and use it in GitHub Desktop.
http request that does a callback request
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
| // http request that does a callback request | |
| var req = function(options) { | |
| var r = http.request(options, function(res) { | |
| var resBody = ''; | |
| res.setEncoding('utf8'); | |
| res.on('data', function (chunk) { | |
| resBody += chunk; | |
| }); | |
| res.on('end', function() { | |
| var resJSON = JSON.parse(resBody); | |
| var callback = resJSON.callback; | |
| if(callback && callback.options) { | |
| var req2 = req(callback.options); | |
| if (callback.data) { | |
| req2.write(JSON.stringify(callback.data)); | |
| } | |
| req2.end(); | |
| } else { | |
| //tasksCreated === false ? doTasks() : allDone(); | |
| } | |
| }); | |
| res.on('error', function(err) { | |
| if (err) { | |
| console.log(err); | |
| } | |
| }); | |
| }); | |
| r.on('error', function(err) { | |
| if (err) { | |
| console.log(err); | |
| } | |
| }); | |
| return r; | |
| }; | |
| var run = function(options, data) { | |
| var qs = querystring.stringify(data); | |
| options.headers['Content-Length'] = qs.length; | |
| var r = req(options); | |
| r.write(qs); | |
| }; | |
| var options = { | |
| host: 'localhost', | |
| port: 5984, | |
| path: '/path/to/update', | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| } | |
| }; | |
| var data = { | |
| from: '+13125551212', // clinic.contact.phone | |
| message: '1!MSBR!2012#1#24#12003212300' + // refid | |
| '#1111#bbbbbbbbbbbbbbbbbbbb#22#8#cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc', | |
| sent_timestamp: '1-19-12 18:45', | |
| sent_to: '+15551212', | |
| message_id: '13579', | |
| foo: 'bar' // extra is ok | |
| }; | |
| run(options, data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment