Accessing API Response Values with Node.js 'request' Package
Assuming you're using the 'request' package in Node.js, you can access the value returned by the API in the callback function of the 'post' method. Here's an example:
const request = require('request');
const options = {
url: 'https://example.com/api',
method: 'POST',
json: true,
body: { key: 'value' }
};
request(options, (error, response, body) => {
if (error) {
console.error(error);
return;
}
// Access the value returned by the API
console.log(body.key);
});
In this example, we send a POST request to 'https://example.com/api' with a JSON body containing a key-value pair. The callback function checks for errors and then logs the API's returned value ('body.key').
Note that the 'request' package is deprecated. Consider using other HTTP client libraries like Axios or node-fetch instead.
原文地址: https://www.cveoy.top/t/topic/l0uS 著作权归作者所有。请勿转载和采集!