Home > database >  Why does this GET request seem to give random characters?
Why does this GET request seem to give random characters?

Time:11-11

This is 1 of my first times using the https node.js module and I tried to make a GET request with this code

const url = new URL("valid url") //I did put a valid url

https.get({
    host: url.hostname,
    path: url.pathname,
    headers: {
        "Content-Type": "application/json"
    }
}, (res) => {
    res.setEncoding("utf8")
    let str = ""
    res.on("data", c => {
        console.log(c)
    })

    res.on("end", () => {
        console.log(str) //this was meant to log the data but I removed the "str  = c" from the data callback
    })
})

But this logs the following:

▼�
�VJ-*�/��LQ�210ЁrsS��‼�S����3KR§2�§�R♂K3�RS�`J�↕sA�I�)�♣�E@NIj�R-��♥g��PP

But I would think .setEncoding("utf8") would work. Is it something to do with the headers? Or maybe the URL object? The result I want is in a <pre> element and is valid JSON.

CodePudding user response:

As said by @James in their comment, the content was compressed. Doing this worked:

import https from "https"

import { createGunzip } from "zlib"

const url = new URL("valid url")

const req = https.get(url, (res) => { //I found out you can use a URL object
    
    let str = "{}"
    let output;
        //See if it's compressed so we can read it properly
        var gzip = createGunzip()
        res.headers["content-encoding"] == "gzip" ? 
        output = res.pipe(gzip) :
        output = res;

    output.on("data", c => {
        str = c.toString()
    })

    output.on("end", () => {
        console.log(JSON.parse(str)) //logs the correct object
    })
})
  • Related