I want to write a script with nodejs
that gets a set of rules from http://hl7.org/fhir
. For example, when I do this request on Postman
(http://hl7.org/fhir/address-use) I get a valid JSON
response:
However, when I use the nodejs
code below which is generated by Postman
, I get an html
as response.
var http = require("http");
var options = {
"method": "GET",
"hostname": "hl7.org",
"port": "80",
"path": "/fhir/address-use",
"headers": {
"content-type": "application/json",
"accept": "application/json"
}
};
var req = http.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
});
req.end();
If you try to go to the url in your browser, it says that the url was moved to another page, but Postman
gets the correct results anyway. I also tried to get the url that it moved, but it returns html
response also.
How can I make my script work just as Postman
?
EDIT:
I tried request
package as recommended in another question. Here is the code:
var request = require("request");
var options = { method: 'GET',
url: 'http://hl7.org:80/fhir/address-use',
headers:
{
accept: 'application/json',
'content-type': 'application/json' } };
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
But the result didn't change. It gets the same html
response.