![]() The body of the HTTP response contains JSON-formatted data, namely an array containing information about users. As before, the fetch() function takes in the URL of the server and awaits a response.Ĭonsole.log( "First user in the array:") Ĭonsole.log( "Name of the first user in the array:") 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. If we get this information on the frontend, we can use it to populate cards or a table or however we would like to display it.Note: The API between node-fetch 3.0 and 2.0 is the same, just the import differs. Opening_crawl: 'It is a period of civil war.\r\n' + Here’s the basic syntax so we can get the response: We just have to use it to send a request and the Fetch API returns a promise under the hood. As a reminder, Promises are asynchronous functions that encapsulate logic into its block of code and returns a response that determines if the promise was resolved or rejected.Īs users of the Fetch API, we don’t need to actually write the promise logic. The Fetch API uses a Promise-based system of communication. Depending on the API, you’ll only be allowed to perform certain requests, mainly to keep their data immutable, so you won’t be able to do those requests that manipulate data, you’ll only be able to read it. In addition, we can hit endpoints on third party APIs. It is where we can Create (POST), Read (GET), Update (PUT), and Destroy (DELETE) data in our database. Other types of requests are POST, PUT, and DELETE. The GET HTTP Request is only one type of request you can make to a server. A GET request hits an endpoint on a server then returns a response with data from that endpoint so you can read it. The Fetch API is an interface that allows us to make HTTP Requests to a server from a web browser like Chrome or Firefox. This article talks about using your browser’s Fetch API to GET some data from an endpoint. Developers often have to retrieve data from their own API or third-party API.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |