Created
August 31, 2017 06:44
-
-
Save winniecluk/10d8b8933bc7b48a1b320a20e8a80a8d 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 PORT = 3000; | |
| var url = require('url'); | |
| var BOUNDARY = '741e90d31eff'; | |
| var crypto = require('crypto'); | |
| var ROOT = 'https://kipuapi.kipuworks.com'; | |
| var URI = '/api/patients'; | |
| var SECRET_KEY = 'QlIn8+DwAKjKVs84P8sJ/qewB8G2zFdMURl4i6KmwqI4SCleQNh6FjOoBiHmXHa1jGar5HyeQmAgkUo6jomjvw=='; | |
| const ACCEPT_HEADER_VALUE = 'application/vnd.kipusystems+json; version=1'; | |
| var fs = require('fs'); | |
| var request = require('request'); | |
| var FileAPI = require('file-api') | |
| , File = FileAPI.File | |
| , FileList = FileAPI.FileList | |
| , FileReader = FileAPI.FileReader; | |
| var fileReader = new FileReader(); | |
| // body: '{"errors":["Attachments attachment has contents that are not what they are reported to be"]}' } | |
| // {"errors":["Attachments attachment has contents that are not what they are reported to be"]} | |
| // _http_outgoing.js:752 | |
| // throw new TypeError('First argument must be a string or Buffer'); | |
| // so NOT a readable stream? | |
| // base64 returns that error | |
| // hex returns that error | |
| // utf-8 returns that unopenable file? | |
| // buffer returns unopenable file? | |
| // escape returns that error | |
| function handleReq(req, res){ | |
| console.log('hitting'); | |
| if (req.url === '/'){ | |
| fs.readFile('simplehtml.html', (err, data) => { | |
| var dataArr = [data]; | |
| var buf = Buffer.concat(dataArr); | |
| console.log(buf); | |
| //int8, uint8, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32 | |
| // var uArr = new Uint32Array(buf); | |
| // console.log(uArr); | |
| postBody = ''; | |
| postBody += '--'+BOUNDARY+'\r\n'; | |
| postBody += 'Content-Disposition: form-data; '; | |
| postBody += 'name="document[data][first_name]"\r\n'; | |
| postBody += '\r\n'; | |
| postBody += 'Trial'; | |
| postBody += '\r\n'; | |
| postBody += '--'+BOUNDARY+'\r\n'; | |
| postBody += 'Content-Disposition: form-data; '; | |
| postBody += 'name="document[data][last_name]"\r\n'; | |
| postBody += '\r\n'; | |
| postBody += '1 offset'; | |
| postBody += '\r\n'; | |
| postBody += '--'+BOUNDARY+'\r\n'; | |
| postBody += 'Content-Disposition: form-data; '; | |
| postBody += 'name="document[recipient_id]"\r\n'; | |
| postBody += '\r\n'; | |
| postBody += '5yQxieStOL4ns95npd1LFSMTrFHqfFskxAX73bNi7A0'; | |
| postBody += '\r\n'; | |
| postBody += '--'+BOUNDARY+'\r\n'; | |
| postBody += 'Content-Disposition: form-data; '; | |
| postBody += 'name="app_id"\r\n'; | |
| postBody += '\r\n'; | |
| postBody += 'oQsSYGt-iYDVq-qX9lGbV6CgMWX9KLvMOX2Qy_PGO7s'; | |
| postBody += '\r\n'; | |
| postBody += '--'+BOUNDARY+'\r\n'; | |
| postBody += 'Content-Disposition: form-data; '; | |
| postBody += 'name="document[attachments_attributes][0][attachment]"; filename="simplehtml.html"\r\n'; | |
| postBody += 'Content-type: text/html \r\n'; | |
| postBody += '\r\n'; | |
| // var dataArr = []; | |
| // var reqObj = {}; | |
| // fileReader.setNodeChunkedEncoding(false); | |
| // fileReader.readAsArrayBuffer(new File('simplepdf.pdf')); | |
| // fileReader.on('data', function(dataChunk){ | |
| // console.log('this is dataChunk'); | |
| // console.log(dataChunk); | |
| // dataArr.push(dataChunk); | |
| // }); | |
| // fileReader.addEventListener('load', function (ev) { | |
| // console.log("dataUrlSize:", ev.target.result.length); | |
| // }); | |
| // fileReader.addEventListener('loadend', function () { | |
| // console.log("Success"); | |
| // console.log('dataArr'); | |
| // console.log(dataArr); | |
| // console.log('dataArr[0]'); | |
| // console.log(dataArr[0]); | |
| // postBody += dataArr[0]; | |
| // postBody += '\r\n'; | |
| // postBody += '--'+BOUNDARY+'--\r\n'; | |
| // | |
| // let md5Digest = crypto.createHash('md5').update(postBody).digest('binary'); | |
| // let contentStr = Buffer.from(md5Digest, 'binary').toString('base64', 0, 24); | |
| // let dateStr = new Date().toUTCString(); | |
| // | |
| // var canonicalStr = 'multipart/form-data, boundary='+BOUNDARY+','; | |
| // canonicalStr += contentStr+','+URI+','+dateStr; | |
| // let signature = crypto.createHmac('sha1', SECRET_KEY).update(canonicalStr).digest('base64'); | |
| // | |
| // let reqObj = { | |
| // url: ROOT + URI, | |
| // method: 'POST', | |
| // headers: { | |
| // 'Content-type': 'multipart/form-data, boundary='+BOUNDARY, | |
| // 'Accept': ACCEPT_HEADER_VALUE, | |
| // 'Authorization': 'APIAuth '+'oQsSYGt-iYDVq-qX9lGbV6CgMWX9KLvMOX2Qy_PGO7s'+':'+signature, | |
| // 'Date': new Date().toUTCString(), | |
| // 'Content-MD5': contentStr | |
| // }, | |
| // body: postBody | |
| // }; | |
| // | |
| // // console.log('this is postBody'); | |
| // // console.log(postBody); | |
| // | |
| // request(reqObj, function(error, response, body){ | |
| // if (error){ | |
| // console.log(error); | |
| // res.writeHead(200, {'content-type':'text/plain'}); | |
| // res.write('error:'); | |
| // res.end(); | |
| // } else if (!error && response.statusCode === 200){ | |
| // console.log('THINGS ARE 200'); | |
| // console.log(body); | |
| // res.writeHead(200, {'content-type':'text/plain'}); | |
| // res.write('received upload:'); | |
| // res.end(); | |
| // } else { | |
| // console.log('THIS IS THE RESPONSE FROM KIPU'); | |
| // console.log(response); | |
| // console.log(body); | |
| // res.writeHead(200, {'content-type':'text/plain'}); | |
| // res.write('response from kipu'); | |
| // res.end(); | |
| // } | |
| // }); | |
| // }); | |
| postBody += buf; | |
| postBody += '\r\n'; | |
| postBody += '--'+BOUNDARY+'--\r\n'; | |
| let md5Digest = crypto.createHash('md5').update(postBody).digest('binary'); | |
| let contentStr = Buffer.from(md5Digest, 'binary').toString('base64', 0, 24); | |
| let dateStr = new Date().toUTCString(); | |
| var canonicalStr = 'multipart/form-data, boundary='+BOUNDARY+','; | |
| canonicalStr += contentStr+','+URI+','+dateStr; | |
| let signature = crypto.createHmac('sha1', SECRET_KEY).update(canonicalStr).digest('base64'); | |
| let reqObj = { | |
| url: ROOT + URI, | |
| method: 'POST', | |
| headers: { | |
| 'Content-type': 'multipart/form-data, boundary='+BOUNDARY, | |
| 'Accept': ACCEPT_HEADER_VALUE, | |
| 'Authorization': 'APIAuth '+'oQsSYGt-iYDVq-qX9lGbV6CgMWX9KLvMOX2Qy_PGO7s'+':'+signature, | |
| 'Date': new Date().toUTCString(), | |
| 'Content-MD5': contentStr | |
| }, | |
| body: postBody | |
| }; | |
| console.log('this is postBody'); | |
| console.log(postBody); | |
| request(reqObj, function(error, response, body){ | |
| if (error){ | |
| console.log(error); | |
| res.writeHead(200, {'content-type':'text/plain'}); | |
| res.write('error:'); | |
| res.end(); | |
| } else if (!error && response.statusCode === 200){ | |
| console.log('THINGS ARE 200'); | |
| console.log(body); | |
| res.writeHead(200, {'content-type':'text/plain'}); | |
| res.write('received upload:'); | |
| res.end(); | |
| } else { | |
| console.log('THIS IS THE RESPONSE FROM KIPU'); | |
| console.log(response); | |
| console.log(body); | |
| res.writeHead(200, {'content-type':'text/plain'}); | |
| res.write('response from kipu'); | |
| res.end(); | |
| } | |
| }); | |
| }); | |
| } | |
| } | |
| var server = http.createServer(handleReq); | |
| server.listen(PORT, function(){ | |
| console.log('server listening'); | |
| }) | |
| // create a pdf w/ one word | |
| // try reading it inside SF | |
| // different string representation | |
| // |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment