Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Why does this GET request seem to give random characters?

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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
    })
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda