Azure IoT HTTP Base Library
The `azure-iot-http-base` package is an internal library within the Azure IoT Node.js SDKs, providing foundational HTTP-specific components and utilities. It is not intended for direct consumption by most application developers; instead, its functionalities are integrated and exposed through higher-level SDK packages such as `azure-iot-device-http` for device clients and `azure-iothub` for service clients. The current stable version is 1.12.3, released as part of the broader Azure SDKs for Node.js update cadence, which typically occurs every few months. Key differentiators include its specialized focus on HTTP operations within the Azure IoT ecosystem, its role as a low-level transport primitive, and its inclusion of TypeScript type definitions, ensuring robust development practices. It abstracts away the complexities of HTTP communication for IoT-specific scenarios, supporting Node.js versions 14.0.0 and above. Developers should generally rely on the orchestrating device or service SDKs for HTTP-based interactions, as this package handles the underlying mechanics.
Common errors
-
Error: The 'url' argument must be of type string. Received an instance of URL
cause Attempting to pass a `URL` object directly to a method expecting a string URL within an older version or specific internal client implementation that hasn't fully adopted `URL` objects.fixEnsure that any URL arguments are converted to strings using `url.toString()` before passing them to internal client methods, for example: `url: myUrl.toString()`. -
TypeError: Cannot read properties of undefined (reading 'statusCode') when handling response.
cause Occurs when a low-level network request fails catastrophically or encounters an unexpected error state, leading to a `response` object being `undefined` or incomplete within the error object.fixImplement robust error handling around network requests, checking for the existence of `error.response` and its properties before accessing them. For example, `if (error.response) { console.error(error.response.statusCode); }`. -
Error: This package has a 'main' entrypoint that is not compatible with ES Modules.
cause While the package is designed for Node.js 14+ and ships TypeScript types, an outdated build configuration or incorrect usage (e.g., trying to `require()` in an ES Module context) might lead to module resolution issues.fixEnsure your Node.js project is configured for ES Modules (`"type": "module"` in `package.json`) and use `import` statements. If using CommonJS, ensure you are using a compatible Node.js version and correct `require()` syntax (though `require` is generally not recommended for modern Azure SDKs).
Warnings
- gotcha This package is an internal library and is NOT intended for direct public consumption. Developers should use higher-level Azure IoT SDKs (e.g., `azure-iot-device-http`, `azure-iothub`) which abstract and manage these HTTP components.
- breaking The minimum required Node.js version was updated from Node.js 12 to Node.js 14. Applications running on older Node.js versions will encounter runtime errors.
- breaking The internal dependency for core HTTP authentication shifted from `@azure/core-http` to `@azure/core-auth` starting from version 1.12.2. While this is primarily an internal change, it signifies a restructuring in how authentication and HTTP client pipelines are managed within the SDK.
- gotcha Recurring dependency vulnerabilities are addressed across releases. While patched regularly, it highlights the importance of keeping Azure SDKs updated to mitigate potential security risks.
Install
-
npm install azure-iot-http-base -
yarn add azure-iot-http-base -
pnpm add azure-iot-http-base
Imports
- HttpsClient
import { HttpsClient } from 'azure-iot-http-base' - HttpClient
import { HttpClient } from 'azure-iot-http-base' - RequestOptions
import { RequestOptions } from 'azure-iot-http-base' - SharedAccessKeyAuthenticationProvider
import { SharedAccessKeyAuthenticationProvider } from 'azure-iot-http-base'
Quickstart
import { HttpsClient, RequestOptions, RequestAndResponse } from 'azure-iot-http-base';
import { URL } from 'url';
// WARNING: This package is an internal library and direct usage is generally discouraged.
// Most developers should use higher-level Azure IoT SDKs like 'azure-iot-device-http'
// or 'azure-iothub' which manage these transport details.
async function makeSimpleHttpRequest() {
console.log('Attempting a low-level HTTP GET request using azure-iot-http-base (for demonstration purposes only)...');
const client = new HttpsClient();
const url = new URL('https://httpbin.org/get?source=azure-iot-http-base-demo');
const requestOptions: RequestOptions = {
method: 'GET',
url: url.toString(),
headers: { 'User-Agent': 'Azure-IoT-HTTP-Base-Demo/1.0' }
};
try {
const response: RequestAndResponse = await client.sendRequest(requestOptions);
console.log(`\nHTTP Status: ${response.statusCode}`);
console.log(`Response Headers: ${JSON.stringify(response.headers, null, 2)}`);
console.log(`Response Body (truncated): ${response.body?.toString().substring(0, 200)}...`);
} catch (error: any) {
console.error(`\nError making HTTP request: ${error.message}`);
if (error.response) {
console.error(`Status: ${error.response.statusCode}`);
console.error(`Body: ${error.response.body?.toString()}`);
}
}
}
makeSimpleHttpRequest().catch(console.error);