0

I need to make a get request to an API that will give me a string that I will then use in my app, however I can't seem to do this easily.

I've done the following but it doesn't work.

function updateClients() {
    var jsonData = "";
    request('http://api.com', function (error, response, body) {
      if (!error && response.statusCode == 200) {
        var jsonData = body; 
      }
    });

    console.log(jsonData);
    io.sockets.emit('update', jsonData);
}

1 Answer 1

2
const http = require('http')
function updateClients() {
   let chunkData = '';
    let request = http.get('http://api.com', function(response) {
        response.on('data', function(chunk) {
            chunkData += chunk;
        });
        response.on('end', function() {
            console.log(chunkData );
            io.sockets.emit('update', chunkData );
        })
    });
    request.on('error', function(error) {
        console.log(error)
    })
    request.end();
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.