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

150
Vistas
Return https.get() response body to client with Nodejs

I am trying to develop a google cloud function that will make an external https GET request and return the response body to the client.

Flow:

  1. client makes request to mockServer function
  2. function makes GET request to example.com
  3. function returns "results" from response body from example.com to client

exports.mockServer = (req, res) => {
    'use strict';
    var https = require('https');
    
    var options = {
        host: 'example.com',
      path: '/path',
      headers: {
        'accept': 'application/json',
        'X-API-Key': 'XXXX'
      }
    };

if (req.method == 'GET'){
https.get(options, function (res) {
        var data = '';
        res.on('data', function (chunk) {
            data += chunk;
        });
        res.on('end', function () {
            if (res.statusCode === 200) {
                  var res_body = JSON.parse(data);
                  var results = JSON.stringify(res_body.result)
                    console.log("results:"+results);
            } else {
                console.log('Status:', res.statusCode);
            }
        });
    }).on('error', function (err) {
          console.log('Error:', err);
    });

} else {
  console.log("Wrong Method");
}   
  res.end()
};

I am able to successfully log the results with console.log("results:"+results); but I cannot figure out how to get it returned to the client. I am still new to this and am learning, so thank you so much in advance to any help!

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

0

Posting @YouthDev's solution as an answer:

Thanks to @DougStevenson and @Deko in the comments, I switched to an axios library and it works like a charm. Thank you to both for pointing me in the correct direction. Below is the working axios code.

exports.mockServer = (req, res) => {
  const axios = require('axios').create({
    baseURL: 'https://example.com'
  });
  return axios.get('/path',{ headers: {'accept': 'application/json','X-API-Key': 'XXXXXX'} })
  .then(response => {
    console.log(response.data);
    return res.status(200).json({
      message: response.data
    })
  })
  .catch(err => {
    return res.status(500).json({
      error: err
    })
  })

};
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