1 | $ wsk action create garbageDetectionAction garbageDetectionAction.js |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | var request = require('request'); var http = require("http"); var querystring = require('querystring'); var utils = { map : function(arr, func){ var res = []; for(var i=0,len=arr.length; i<len; i++){ res.push(func(arr)); } return res; }, encodeStrings : function(arr){ return utils.map(arr, querystring.escape); }, decodeStrings : function(arr){ return utils.map(arr, decodeURI); }, }; function main(params) { // payload is urls with ',' split, such as "url1,url2" var payload = params["payload"].split(',') || []; var data=[]; for(var i=0,len=payload.length; i<len; i++){ data.push({'url': payload}); } console.log(data); var options = { host: 'garbagecodedetection.mybluemix.net', port: 80, path: '/rest/garbagechar_scan', method: 'POST', headers: { 'Content-Type': 'application/json', } }; var req = http.request(options, function (res) { res.on('data', function (message) { var ret= eval('(' + message + ')'); console.log('response : ' ,ret); console.log('response type: ', typeof ret); for(var i=0; i<ret.length; i++) { ret['garbled_lines'] = utils.encodeStrings(ret['garbled_lines']); } console.log('Encoded response: ', ret) whisk.done({result: ret}); }); }); req.on('error', function(e) { console.log('problem with request: ' + e.message); whisk.done({text: 'Problem with request' + e.message}); }); // write data to request body req.write(JSON.stringify(data)); req.end(); return whisk.async(); } |
1 | $ wsk action invoke garbageDetectAction --blocking --result -p payload |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |