nodejs call api get request
nodejsTo call a GET API from a Node.js application, you can use the built-in http or https modules to make an HTTP request to the API endpoint. Here’s an example:
const https = require('https');
const options = {
hostname: 'jsonplaceholder.typicode.com',
path: '/todos/1',
method: 'GET'
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
});
req.on('error', error => {
console.error(error);
});
req.end();
In this example, we’re using the https module to make a GET request to the ”https://jsonplaceholder.typicode.com/todos/1” endpoint. We’re setting the options for the request, including the hostname, path, and method. We’re then creating the request object with https.request() and passing in the options. We’re also setting up a listener for the response’s “data” event, which will output the response data to the console. Finally, we’re sending the request with req.end().
You can run this code in a Node.js environment (e.g. in a JavaScript file or in a Node.js REPL) to see the response from the API. You can modify the options to call a different API endpoint or use the http module instead of https if the API is served over HTTP instead of HTTPS.
Other Article on Tag nodejs
- - axios Request path contains unescaped characters
- - CryptoJS.HmacSHA256 Cannot read properties of undefined (reading 'sigBytes')
- - encrypt by java and decrypt by node
- - fetch function mode sors, no-cors, same-origin
- - How to set value for NODE_TLS_REJECT_UNAUTHORIZED
- - it error Request path contains unescaped characters
- - java vs nodejs
- - nodejs add redis
- - nodejs call api get request
- - nodejs connect mysql database