1/12/2023 0 Comments Nodejs http requestLet's add a new item to that list for the user whose id equals 123. We'll demonstrate how this works by adding a new item to JSONPlaceholder's todos. These fields have straight-forward meanings: method sets what type of HTTP request we're using ( POST in our case), body contains the body/data of our request, and headers contains all the necessary headers, which in our case is just the Content-Type so there isn't any confusion when parsing our request.įor a full list of options, you can visit the documentation. There are many possible options we can set using this parameter, but the only ones we'll use in this article are method, body and headers. Without this optional parameter, our request is a GET request, by default. As we mentioned earlier, the fetch() function allows for an additional parameter to be added to make POST requests to a web server. We can also use the fetch() function to post data instead of retrieving it. We also could have printed the entire JSON returned by res.json(). Running this program would give us: First element in the array:ĬatchPhrase: 'Multi-layered client-server neural-net', json() function, and this allowed us to easily access individual elements and their fields. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |