PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/httpreq/examples.js

https://gitlab.com/prova/mha_email
JavaScript | 214 lines | 173 code | 17 blank | 24 comment | 14 complexity | a687357130e24520a0c42beefebb6095 MD5 | raw file
  1. var httpreq = require('./lib/httpreq');
  2. fs = require('fs')
  3. // example1(); // get www.google.com
  4. // example2(); // do some post
  5. // example3(); // same as above + extra headers + cookies
  6. // example4(); // https also works:
  7. // example5(); // uploading some file:
  8. // example6(); // u can use doRequest instead of .get or .post
  9. // example7(); // download a binary file:
  10. // example8(); // send json
  11. // example9(); // send your own body content (eg. xml)
  12. // example10(); // set max redirects:
  13. // example11(); // set timeout
  14. // example12(); // // download file directly to disk
  15. // get www.google.com
  16. function example1(){
  17. httpreq.get('http://www.google.com', function (err, res){
  18. if (err){
  19. console.log(err);
  20. }else{
  21. console.log(res.headers); //headers are stored in res.headers
  22. console.log(res.body); //content of the body is stored in res.body
  23. }
  24. });
  25. }
  26. // do some post
  27. function example2(){
  28. httpreq.post('http://posttestserver.com/post.php', {
  29. parameters: {
  30. name: 'John',
  31. lastname: 'Doe'
  32. }
  33. }, function (err, res){
  34. if (err){
  35. console.log(err);
  36. }else{
  37. console.log(res.body);
  38. }
  39. });
  40. }
  41. // same as above + extra headers + cookies
  42. function example3(){
  43. httpreq.post('http://posttestserver.com/post.php', {
  44. parameters: {
  45. name: 'John',
  46. lastname: 'Doe'
  47. },
  48. headers:{
  49. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:18.0) Gecko/20100101 Firefox/18.0'
  50. },
  51. cookies: [
  52. 'token=DGcGUmplWQSjfqEvmu%2BZA%2Fc',
  53. 'id=2'
  54. ]
  55. }, function (err, res){
  56. if (err){
  57. console.log(err);
  58. }else{
  59. console.log(res.body);
  60. }
  61. });
  62. }
  63. // https also works:
  64. function example4(){
  65. httpreq.get('https://graph.facebook.com/19292868552', function (err, res){
  66. if (err){
  67. console.log(err);
  68. }else{
  69. console.log(JSON.parse(res.body));
  70. }
  71. });
  72. }
  73. // uploading some file:
  74. function example5(){
  75. httpreq.uploadFiles({
  76. url: "http://rekognition.com/demo/do_upload/",
  77. parameters:{
  78. name_space : 'something',
  79. },
  80. files:{
  81. fileToUpload: __dirname + "/test/testupload.jpg"
  82. }},
  83. function (err, res){
  84. if (err) return console.log(err);
  85. console.log(res.body);
  86. });
  87. }
  88. // u can use doRequest instead of .get or .post
  89. function example6(){
  90. httpreq.doRequest({
  91. url: 'https://graph.facebook.com/19292868552',
  92. method: 'GET',
  93. parameters: {
  94. name: 'test'
  95. }
  96. },
  97. function (err, res){
  98. if (err){
  99. console.log(err);
  100. }else{
  101. console.log(JSON.parse(res.body));
  102. }
  103. });
  104. }
  105. // download a binary file:
  106. function example7(){
  107. httpreq.get('https://ssl.gstatic.com/gb/images/k1_a31af7ac.png', {
  108. binary: true,
  109. progressCallback: function (err, progress) {
  110. console.log(progress);
  111. }
  112. },
  113. function (err, res){
  114. if (err){
  115. console.log(err);
  116. }else{
  117. fs.writeFile(__dirname + '/test.png', res.body, function (err) {
  118. if(err) return console.log("error writing file");
  119. });
  120. }
  121. });
  122. }
  123. // send json
  124. function example8(){
  125. httpreq.post('http://posttestserver.com/post.php',{
  126. json: {name: 'John', lastname: 'Do'},
  127. headers:{
  128. 'Content-Type': 'text/xml',
  129. }},
  130. function (err, res) {
  131. if (err){
  132. console.log(err);
  133. }else{
  134. console.log(res.body);
  135. }
  136. }
  137. );
  138. }
  139. // send your own body content (eg. xml):
  140. function example9(){
  141. httpreq.post('http://posttestserver.com/post.php',{
  142. body: '<?xml version="1.0" encoding="UTF-8"?>',
  143. headers:{
  144. 'Content-Type': 'text/xml',
  145. }},
  146. function (err, res) {
  147. if (err){
  148. console.log(err);
  149. }else{
  150. console.log(res.body);
  151. }
  152. }
  153. );
  154. }
  155. // set max redirects:
  156. function example10(){
  157. httpreq.get('http://scobleizer.com/feed/',{
  158. maxRedirects: 2, // default is 10
  159. headers:{
  160. 'User-Agent': 'Magnet', //for some reason causes endless redirects on this site...
  161. }},
  162. function (err, res) {
  163. if (err){
  164. console.log(err);
  165. }else{
  166. console.log(res.body);
  167. }
  168. }
  169. );
  170. }
  171. // set timeout
  172. function example11(){
  173. httpreq.get('http://localhost:3000/',{
  174. timeout: (5 * 1000) // timeout in milliseconds
  175. },
  176. function (err, res) {
  177. if (err){
  178. console.log(err);
  179. }else{
  180. console.log(res.body);
  181. }
  182. }
  183. );
  184. }
  185. // download file directly to disk:
  186. function example12 () {
  187. httpreq.download(
  188. 'https://ssl.gstatic.com/gb/images/k1_a31af7ac.png',
  189. __dirname + '/test.png'
  190. , function (err, progress){
  191. if (err) return console.log(err);
  192. console.log(progress);
  193. }, function (err, res){
  194. if (err) return console.log(err);
  195. console.log(res);
  196. });
  197. }