0

So, i'm trying to get the http status code of an URL that i pass to the funcion. so far i've used the request module but the problem is that request fetches all the content of the given URL and if the content is a big amount of data it's slow, but i just need to get the status code to make an if statement.

request(audioURL, function (error, response) {
    if (!error && response.statusCode == 200) {
        queue.push({
            title: videoTitle, 
            user: message.author.username, 
            mention: message.author.mention(), 
            url: audioURL
        });

        bot.reply(message, "\"" + videoTitle + "\" has been added to the queue.")
    }
    else {
        bot.reply(message, "Sorry, I couldn't get the audio track for that video. HTTP status code: " + response.statusCode);
    }
});

This is what i got so far.audioURL is a basic string with a link to a media file

1 Answer 1

2

http.request can take an options Object as its first parameter. Use something like this:

const options = {
    method: 'HEAD',
    host: 'example.com',
};

const req = http.request(options, (res) => {
    console.log(JSON.stringify(res.headers));
});

req.end();

The problem is that you'll want to turn your audioURL into its components, to pass as parameters of options. I'm using the HEAD method, that fetches only the headers of your request.

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.