make a http head request in typescript

Here is an example of how to make an HTTP HEAD request in TypeScript using the Node.js request library:

index.ts
import * as request from 'request';

const url = 'https://www.example.com';

request.head(url, function (error, response) {
  if (error) {
    console.error(error);
  } else {
    console.log(response.statusCode, response.headers);
  }
});
240 chars
12 lines

This code imports the request library and uses the head function to make an HTTP HEAD request to the specified URL. The head function takes a callback function that is called once the request is complete. If an error occurs, the error is logged to the console. Otherwise, the status code and headers of the response are logged to the console.

Alternatively, you can use Promises to handle the response asynchronously:

index.ts
import * as request from 'request-promise';

const url = 'https://www.example.com';

request.head(url)
  .then(function (response) {
    console.log(response.statusCode, response.headers);
  })
  .catch(function (error) {
    console.error(error);
  });
254 chars
12 lines

In this example, the request-promise library is used instead of request. The head function returns a Promise that is fulfilled with the response object when the request completes. If an error occurs, the Promise is rejected with the error object. The then method is used to handle the response, and the catch method is used to handle errors.

gistlibby LogSnag