{"id":1600,"date":"2016-04-05T21:48:49","date_gmt":"2016-04-05T21:48:49","guid":{"rendered":"http:\/\/www.phpmind.com\/blog\/?p=1600"},"modified":"2016-04-05T21:48:49","modified_gmt":"2016-04-05T21:48:49","slug":"node-rest-client","status":"publish","type":"post","link":"https:\/\/www.phpmind.com\/blog\/2016\/04\/node-rest-client\/","title":{"rendered":"Node Rest Client"},"content":{"rendered":"<p><strong>Making REST calls<\/strong><br \/>\nEach API call takes a set of data in JSON format and returns data also in JSON format. The exception to this is a GET request, which instead takes its data as a query string. I didn&#8217;t want to have to deal with this detail, so I cobbled together a wrapper function that would produce data in the correct format and issue the request.<\/p>\n<pre>\r\nvar querystring = require('querystring');\r\nvar https = require('https');\r\n\r\nvar host = 'www.thegamecrafter.com';\r\nvar username = 'JonBob';\r\nvar password = '*****';\r\nvar apiKey = '*****';\r\nvar sessionId = null;\r\nvar deckId = '68DC5A20-EE4F-11E2-A00C-0858C0D5C2ED';\r\n\r\nfunction performRequest(endpoint, method, data, success) {\r\n  var dataString = JSON.stringify(data);\r\n  var headers = {};\r\n  \r\n  if (method == 'GET') {\r\n    endpoint += '?' + querystring.stringify(data);\r\n  }\r\n  else {\r\n    headers = {\r\n      'Content-Type': 'application\/json',\r\n      'Content-Length': dataString.length\r\n    };\r\n  }\r\n  var options = {\r\n    host: host,\r\n    path: endpoint,\r\n    method: method,\r\n    headers: headers\r\n  };\r\n\r\n  var req = https.request(options, function(res) {\r\n    res.setEncoding('utf-8');\r\n\r\n    var responseString = '';\r\n\r\n    res.on('data', function(data) {\r\n      responseString += data;\r\n    });\r\n\r\n    res.on('end', function() {\r\n      console.log(responseString);\r\n      var responseObject = JSON.parse(responseString);\r\n      success(responseObject);\r\n    });\r\n  });\r\n\r\n  req.write(dataString);\r\n  req.end();\r\n}\r\n<\/pre>\n<p>https:\/\/rapiddg.com\/blog\/calling-rest-api-nodejs-script<\/p>\n<p>https:\/\/www.npmjs.com\/package\/node-rest-client<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making REST calls Each API call takes a set of data in JSON format and returns data also in JSON format. The exception to this is a GET request, which instead takes its data as a query string. I didn&#8217;t want to have to deal with this detail, so I cobbled together a wrapper function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-1600","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/comments?post=1600"}],"version-history":[{"count":1,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1600\/revisions"}],"predecessor-version":[{"id":1601,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/posts\/1600\/revisions\/1601"}],"wp:attachment":[{"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/media?parent=1600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/categories?post=1600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpmind.com\/blog\/wp-json\/wp\/v2\/tags?post=1600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}