Node.js HTTP Requests In Depth

Node.js HTTP Requests In Depth

Node.js has nice modules for making HTTP and HTTPS requests. You can see a basic introduction in this post, but here’s a quick overview.

 

 

HTTP Overview

HTTP requests allow a program to interact with resources on a server using four kinds of request methods: “POST”, “PUT”, “DELETE”, and “GET”. Now, there are more, but these are the most used, “GET” especially. So, “POST” and “PUT” write information to the server through the request, “DELETE” removes information from the server at the specified URI(Universal Resource Identifier), while “GET” returns information from the server in a variety of ways. Now, that variety is today’s topic; we’ll be discussing search queries and headers. So, let’s start with queries.

 

 

Search Queries

Now, most “GET” requests just make a request to a specific URL on a server.

Here’s a stripped down example using Node.js from a project of mine.

Here we make a “GET” request to the server and it returns a JSON object with information about a cat picture. Here’s the output from my Discord Bot, that implements this code:

And here’s the Node.js output

As you can see I only used the file field for displaying the picture in Discord. But, not all requests are that simple; this is where search queries come in.

Search queries are special characters that can be put at the end of the URL to specify the information you receive from the server. For example, an API might require that you specify the format of the information (XML, JSON, etc) returned. Thus, we must add that information inside of our URL. Here’s an example using a quote API.

Here, we add a “?” symbol at the end of the URL and the parameters that follow, separated by an “&” symbol. The “?” symbol tells the request that we are querying and the “&” separates parameters. The parameter structure may change between APIs, but the “?” and “&” symbol is a staple in the format. Now, here’s the output from my Discord bot.

A nice quote, but these aren’t the only things you can set with HTTP request, you can also set headers.

Headers

Headers are like the title/billboards of your request; they give the server information on who made the request, what kind of content they require, and more. You can set any kind of headers you want in your request, but most APIs may require certain headers to be set before allowing your request through.

Now, here’s an example of using an API that requires the developer to enter header information.

Here we set two headers that are required for the API to give us access to their information; req.setHeader sets that information for us. We also set our query settings, and as you can see the parameter format is different from the simple examples from before. This is an important change, otherwise, the API would deny our request. For larger APIs the same is true. Anyway, here’s the output of the second request from my bot after parsing the information.

 

This was just covering ‘GET” requests, but these also apply to all other forms of requests.

I hope this improves your coding skills; there’s a lot that can be done with this information (Twitter in RPGMakerMV anyone? Or maybe high scores stored on a server).

 

Take care!

P.S. I’ll be releasing this bot for public use in the future stay tuned!

Sources

https://nodejs.org/api/http.html

https://nodejs.org/api/https.html

http://docs.kitsu.apiary.io/#introduction/json-api/request-headers

Leave a Reply

Your email address will not be published.