3

I'm looking for a definitive way to handle simple HTTP requests to a REST API using modern javascript frameworks, in my case Typescript but I guess it would also apply to Nodejs.

Since there doesn't seem to be a simple native way to do this, I have found a plethora of libraries, some now deprecated and dozen's of articles dating back several years some with updates based on the newer best practises. Surely it can't be that hard. This takes me about 5mins to implement in Golang or Python, but alas with JavaScript its a pain - Is it just me?

Could somebody please clarify the current state of play and recommended way to do this based on where we are now in 2020.

5
  • Javascript, the language, does not contain http support - it doesn't even have networking builtin. Python doesn't really either - you get stuff like this from a library, some more standard than others. So, you pick a specific environment and then use the tools in that environment. In the browser, you would use fetch(). In node.js, you can use the built-in http.request() or there are higher level and more feature-rich libraries such as got() or superAgent() or axios(). A good list of options is here. I'm using got(). Commented Apr 22, 2020 at 8:02
  • One thing that is a bit different with node.js than I've seen with other language environments that have been around awhile is that the open source NPM library system is so incredibly rich and trivial to use that the node.js developers often just decide not to add something into the base platform because there are so many good third party options that are already there. Having such a rich open source system from the early days of the platform probably led node.js down a bit different path than something like python which probably had to build in more themselves in the beginning. Commented Apr 22, 2020 at 8:09
  • Thanks @jfriend00. This all makes sense. I'll take a look at nodes http.request() Commented Apr 22, 2020 at 8:12
  • I might also add that request and request-promise were kind of the defacto standard for a long time and many nodejs tutorials show them and teach them and for years if you followed a lot of node.js code, you would seem them used a lot. As it sounds like you know, they have been deprecated in the last year for a variety of reasons so that recently opened up the playing field to all the other options. I imagine one or two "winners" will emerge and again become the favorites that many people encounter first, though there are many good options. Commented Apr 22, 2020 at 8:13
  • Your question is turning into a solicitation for third party libraries which is off-topic here for a variety of reasons. I don't know if you intended for that to be what happens, but that is what the answers are showing. Commented Apr 22, 2020 at 8:21

3 Answers 3

3

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(yourUrl);
} catch (exception) {
    process.stderr.write(`ERROR received from ${url}: ${exception}\n`);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Look at the light lib typed-rest-client officially supported by Microsoft

Comments

1

Do checkout https://www.npmjs.com/package/node-fetch and https://www.npmjs.com/package/axios

    // using axios
const options = {
  url: 'http://localhost/test.htm',
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=UTF-8'
  },
  data: {
    a: 10,
    b: 20
  }
};

axios(options)
  .then(response => {
    console.log(response.status);
  });



//using fetch
const url = 'http://localhost/test.htm';
const options = {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=UTF-8'
  },
  body: JSON.stringify({
    a: 10,
    b: 20
  })
};

fetch(url, options)
  .then(response => {
    console.log(response.status);
  });

4 Comments

Thanks @nitrocode - I will look at these. Shame there isn't a goto single source of truth that covers latest best practise
@user1513388 - There's no "best practice" way to make an http request in any language. It depends upon your needs. Sometimes you want a stream interface, sometimes you want oauth support. Sometimes you want to be able to parse rare mime types. Sometimes you want a certain kind of proxy support. There are thousands of features possible with an http request. There is no single source of truth when selecting any library like this. It sounds like you want someone to just dictate what you use regardless of your requirements. That's not how an open platform works. You have choices.
Yea you are right @jfriend00 it all depend on the current need
Thanks - I get it. Again great feedback and very useful. This information at least enables the narrowing of the options.

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.