Http Request in TypeScript

Even though request-promise-native probably works just fine, Axios is a way better alternative for use in TypeScript. It comes with its own type definitions and is overall less dependent on other packages. Using it's API is quite like the answer provided by Adrian, however there are a few subtle differences.

const url: string = 'your-url.example';

try {
    const response = await axios.get(url);
} catch (exception) {
    process.stderr.write(`ERROR received from ${url}: ${exception}\n`);
}

Obviously you can leave out the try/catch statement if you want the exception to be handled by the client.


I would suggest using https://github.com/node-fetch/node-fetch

import fetch from 'node-fetch';

const response = await fetch('https://api.github.com/users/github');
const data = await response.json();

console.log(data);

For POST request:

import fetch from 'node-fetch';

const response = await fetch('https://bin.org/post', {method: 'POST', body: 'a=1'});
const data = await response.json();

console.log(data);