1/2/2024 0 Comments Nodejs http request![]() We will retrieve fake user data from the JSONPlaceholder REST API. We'll get the entire HTML markup of the Google home page logged to the console: Fetching JSON Data From REST APIĪnother common use case for the node-fetch module is getting data using the REST API. If we run the code above from the console: $ node index.js Here we're waiting for the result of the previous conversion and printing it to the console. In this line, we're waiting to receive the response from the Google web server and converting it to text format. then() functions to help us manage the response and data from our request. Because node-fetch is promise-based, we're chaining a couple of. The only parameter we've added to the fetch() function is the URL of the server we're making an HTTP request to. In the code above, we're loading the node-fetch module and then fetching the Google home page. ![]() Let's make a simple GET request to Google's home page: fetch( '') Next, install node-fetch as shown above and add an index.js file. This will create a package.json file in the directory. The node-fetch package allows you to do all of that.Ĭreate a directory for your project, cd into the directory and initialize a Node project with default settings: $ npm init -y You might want to retrieve text from the web server, a whole web page, or data from using REST API. There are two common use cases of fetching data from a web server. headers - an object containing response headers, a specific header can be accessed using the get() function.ok - equals true if status is a 2xx status code (a successful request).status and statusText - contain information about the HTTP status code.json() - parses the response body into a JSON object, and throws an error if the body can't be parsed.text() - returns the response body as a string.The function returns a Response object that contains useful functions and information about the HTTP response, such as: The optional options parameter is used when we want to use fetch() for anything other than a simple GET request, but we will talk about that more in-depth later. It has to be an absolute URL or the function will throw an error. The url parameter is simply the direct URL to the resource we wish to fetch. To install a specific version of the module, you can use npm: $ npm install Īs previously mentioned, the fetch() function in the node-fetch module behaves very similarly to the native window.fetch() function. This method returns the value of param name when present.Note: The API between node-fetch 3.0 and 2.0 is the same, just the import differs. With Content-Type: text/html charset=utf-8 This method returns true if the incoming request’s "Content-Type" HTTP header field matches the MIME type specified by the type parameter. This method returns the specified HTTP request header field. This method checks if the specified content types are acceptable, based on the request’s Accept HTTP header field. Request Object Methods req.accepts(types) Indicates whether the request is "stale," and is the opposite of req.fresh.Īn array of subdomains in the domain name of the request.Ī Boolean value that is true if the request’s "X-Requested-With" header field is “XMLHttpRequest”, indicating that the request was issued by a client library such as jQuery. When using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use. The request protocol string, "http" or "https" when requested with TLS.Īn object containing a property for each query string parameter in the route.Ī Boolean that is true if a TLS connection is established. Ĭontains the path part of the request URL. For example, if you have the route /user/:name, then the "name" property is available as. This property is much like req.url however, it retains the original request URL, allowing you to rewrite req.url freely for internal routing purposes.Īn object containing properties mapped to the named route “parameters”. When the trust proxy setting is true, this property contains an array of IP addresses specified in the “X-Forwarded-For” request header. Indicates whether the request is "fresh." It is the opposite of req.stale.Ĭontains the hostname from the "Host" HTTP header. When using cookie-parser middleware, this property is an object that contains cookies sent by the request. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser The URL path on which a router instance was mounted.Ĭontains key-value pairs of data submitted in the request body. This property holds a reference to the instance of the express application that is using the middleware. Request Object Propertiesįollowing is the list of few properties associated with request object. The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |