javascript - How to pipe a HTTPS get response into an Object - Stack Overflow

I transformed my code so that instead of requiring an extra node_modules, I could just use some HTTPS G

I transformed my code so that instead of requiring an extra node_modules, I could just use some HTTPS GET requests, the problem is that when I try to pipe /releases/ which is basically a raw JSON file, my code requires it back and issues occur like SyntaxError: Unexpected end of JSON input, because for some reason, when I console.log() the so called JSON array, the end isn't pleted with ] or }. So I try to pipe the response into an array, but now I get an error: dest.on isn't a function,

Code:

    https.get({hostname: `api.github`, path: `/repos/${username}/${reponame}/releases`, headers: {'User-Agent': 'a user agent'}}, async (response) => {
        var file = new Array()
        response.pipe(file)
        response.on('end', async function() { //issue occurs at response.pipe ???
        var releases = JSON.parse(fs.readFileSync('./releases.json', 'utf8'))
        console.log(releases)

The JSON file that I access from Github looks like: (random repository)

But, my file (releases.json) looks like this

Edit: I did extensive testing. I used the same JSON file my pkg outputted, read it with fs and so on, and everything seems fine. So the issue is most likely with https / response

I transformed my code so that instead of requiring an extra node_modules, I could just use some HTTPS GET requests, the problem is that when I try to pipe /releases/ which is basically a raw JSON file, my code requires it back and issues occur like SyntaxError: Unexpected end of JSON input, because for some reason, when I console.log() the so called JSON array, the end isn't pleted with ] or }. So I try to pipe the response into an array, but now I get an error: dest.on isn't a function,

Code:

    https.get({hostname: `api.github.`, path: `/repos/${username}/${reponame}/releases`, headers: {'User-Agent': 'a user agent'}}, async (response) => {
        var file = new Array()
        response.pipe(file)
        response.on('end', async function() { //issue occurs at response.pipe ???
        var releases = JSON.parse(fs.readFileSync('./releases.json', 'utf8'))
        console.log(releases)

The JSON file that I access from Github looks like: https://api.github./repos/davidmerfield/randomColor/releases (random repository)

But, my file (releases.json) looks like this

Edit: I did extensive testing. I used the same JSON file my pkg outputted, read it with fs and so on, and everything seems fine. So the issue is most likely with https / response

Share Improve this question edited Mar 11, 2020 at 7:48 SomePerson asked Mar 10, 2020 at 17:52 SomePersonSomePerson 1,3095 gold badges19 silver badges48 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I found out how to pipe the HTTP request into an object, instead of piping it into a file. Thanks to this post. I did that and turned the string into a JSON array.

https.get({hostname: `api.github.`, path: `/repos/${username}/${reponame}/releases`, headers: {'User-Agent': 'agent'}}, async response => {
        var str = ''
        response.on('data', (data) => {
            str += data
        })
        response.on('end', async function() {
        var releases = JSON.parse(str)
//and so on...

You can require JSON files. So, if you need this file, you can do something like:

const releases = require('./releases.json');

You do not need to read it with fs, unless you really want to.

TypeError: dest.on is not a function

This error will be thrown if you try to pipe to non-Writable Stream object. Check here

Which in this case Array is not a Writable Stream. You can create a writable stream using fs.createWriteStream() and pipe the response to it.

https.get(
  { hostname: `api.github.`, path: `/repos/${username}/${reponame}/releases`, headers: { "User-Agent": "a user agent" } },
  async response => {
    const writableStreamFile = fs.createWriteStream("./releases.json");
    response.pipe(writableStreamFile);
    response.on("end", async function() {
      var releases = JSON.parse(fs.readFileSync("./releases.json", "utf8"));
      console.log(releases);
    });
  }
);


发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744729684a4590392.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信