To make this easier to use, each of the request methods can pipe their output to another stream. In short, using streams for large amounts of data (like files) can help reduce your app's memory footprint and response time. For a short description on how and why you should use them, check out the "Streams" section of the Node HTTP Servers for Static File Serving article. Their usefulness extends beyond just network requests, but this serves as a perfect example as to why you should use them. One of the most under-used features in many programming languages, in my opinion, are streams. For more info on these extra options, check out this section of the request README. While this will be more than sufficient for most users' use-cases, there are times where you need even more fine-grained control, like pre/post CLRFs (new-lines), chunking, or specifying your own multiparts. This will send your files with a MIME type of multipart/form-data, which is a multipart form upload. Request(options, function( err, res, body), callback) form: An object containing key-value form data.headers: An object of HTTP headers (key-value) to be set in the request.method: The HTTP method to be used (GET, POST, DELETE, etc).url: The destination URL of the HTTP request.Here are some of the more common options you'll encounter in your applications: The first argument to request can either be a URL string, or an object of options. This type of request works for any HTTP endpoint, whether it returns HTML, JSON, an image, or just about anything else. The code above submits an HTTP GET request to and then prints the returned HTML to the screen. The "hello world" example for this library is as easy as passing a URL and a callback: const request = require( 'request') While there are quite a few options available to you in request (many of which we'll cover throughout this article), it can be pretty simple to use as well. Actually, it is really just a wrapper around Node's built in http module, so you can achieve all of the same functionality on your own with http, but request just makes it a whole lot easier. The request module is by far the most popular (non-standard) Node package for making HTTP requests. Either way, you'll be making a lot of HTTP requests, which means you'll need a solid module to make writing the code much more bearable. Using Node/JavaScript is one of the most popular languages to handle applications like this. These days our web applications tend to have a lot of integrations with other services, whether it be interacting with a REST service like Twitter, or downloading images from Flickr.
0 Comments
Leave a Reply. |