2

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:

enter image description here

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.

Bünyamin Sarıgül
  • 3,031
  • 4
  • 30
  • 55
  • Why are you setting `Content-Type` on a GET request? There is no request body to describe the type of. `cache-control` is a response header, it has no place on a request. – Quentin Mar 28 '17 at 08:18
  • Because I am tired of getting `html`, I can put any header that contains `json` now :) I know it is not required but I don't think it cause a problem. – Bünyamin Sarıgül Mar 28 '17 at 08:21
  • Then you probably implemented the answers incorrectly. – Quentin Mar 28 '17 at 08:41
  • The answer is "Use `request`". `Postman` generates code also for `request` package. So, I don't think I implemented it incorrectly. – Bünyamin Sarıgül Mar 28 '17 at 08:44
  • You can also use axios: `axios.get('http://hl7.org/fhir/address-use/', {headers: {'Accept': 'application/json'}})`, which follow redirect automatically (5 times max by default) – shaochuancs Mar 28 '17 at 08:53
  • By default, Postman will follow the Location header in all 301/302 responses automatically. To make a test, you can set the **Automatically follow redirects** setting to No. – shaochuancs Mar 28 '17 at 08:57
  • @BünyaminSarıgül — There are several answers. One of them says "use request", but you are using http.request, not request. – Quentin Mar 28 '17 at 08:58
  • @Quentin I assume you didn't see the edited part of the question. I used request. – Bünyamin Sarıgül Mar 28 '17 at 09:09
  • @shaochuancs `axios` not logging any response (not even error), I couldn't make it work – Bünyamin Sarıgül Mar 28 '17 at 10:14
  • @BünyaminSarıgül `axios.get` returns Promise object. In order to get response/error, you need to register callback function with `.then` or `.catch`: `axios.get(...).then(function(res) {console.log(res.data)})` – shaochuancs Mar 28 '17 at 10:19
  • @shaochuancs yes, I did that just like in its documentation. But not responded anything. I wonder if it has no connection timeout and keep trying to connect but fails. – Bünyamin Sarıgül Mar 28 '17 at 10:21
  • hmmm, I guess maybe it's due to your network environment? – shaochuancs Mar 28 '17 at 10:34
  • @shaochuancs still there is no problem on `postman` – Bünyamin Sarıgül Mar 28 '17 at 10:41
  • I also had a problem with redirects and node request even though followAllRedirects was set to true in options. Ran across article that said set these options instead simple = false resolveWithFullResponse = true I also set follow all redirects to false – dan Jul 06 '19 at 13:11

0 Answers0