I'm trying to get JSON data from a data provider with terrible, terrible documentation. Their API's response has a content-type of 'application/octet-stream' which, if I use my standard API consumption approach, ends-up serving me with a file containing the data... and setting Accept to 'application/json' in my Request does not make any difference. So I'm trying to transform the octet-stream in JSON on my side and this is what I managed to do:
const request = require("request");
module.exports.test = function() {
var text = "";
var url = "XXX";
request
.get({
url: url,
headers: {
Accept: "application/json"
}
})
.on("response", function(response) {
response.on("data", function(data) {
text = text + data.toString();
});
})
.on("end", function() {
console.log(text); //This is where I stopped...
});
};
That's where I stopped, the JSON is printed in the log and so I could go on with my life and use the amazingly well named variable 'text' to do what I need to do with the data... BUT:
- Is this the best way for dealing with octet-stream responses?
- Should I trust a data provider (and pay for their data) that is not able to provide an API with responses other than application/octet-stream ?