What is @azure/core-http?
@azure/core-http is a foundational library for making HTTP requests and handling responses in Azure SDKs. It provides a set of utilities and abstractions to simplify the process of interacting with HTTP services, including features like request and response handling, middleware support, and authentication.
What are @azure/core-http's main functionalities?
Making HTTP Requests
This feature allows you to make HTTP requests using the DefaultHttpClient and WebResource classes. The code sample demonstrates how to make a GET request to a public API and log the response.
const { DefaultHttpClient, WebResource } = require('@azure/core-http');
async function makeRequest() {
const client = new DefaultHttpClient();
const request = new WebResource('https://jsonplaceholder.typicode.com/posts', 'GET');
const response = await client.sendRequest(request);
console.log(response.bodyAsText);
}
makeRequest();
Handling Middleware
This feature allows you to add middleware to the HTTP pipeline. The code sample demonstrates how to add a logging middleware that logs HTTP requests and responses.
const { DefaultHttpClient, WebResource, HttpPipelineLogger, HttpPipelineLogLevel } = require('@azure/core-http');
async function makeRequestWithMiddleware() {
const client = new DefaultHttpClient({
requestPolicyFactories: [
(nextPolicy, options) => new HttpPipelineLogger(nextPolicy, options, HttpPipelineLogLevel.INFO)
]
});
const request = new WebResource('https://jsonplaceholder.typicode.com/posts', 'GET');
const response = await client.sendRequest(request);
console.log(response.bodyAsText);
}
makeRequestWithMiddleware();
Authentication
This feature allows you to handle authentication by using TokenCredentials. The code sample demonstrates how to make an authenticated GET request by setting the Authorization header with a bearer token.
const { DefaultHttpClient, WebResource, TokenCredentials } = require('@azure/core-http');
async function makeAuthenticatedRequest() {
const token = 'YOUR_ACCESS_TOKEN';
const credentials = new TokenCredentials(token);
const client = new DefaultHttpClient();
const request = new WebResource('https://jsonplaceholder.typicode.com/posts', 'GET');
request.headers.set('Authorization', `Bearer ${credentials.token}`);
const response = await client.sendRequest(request);
console.log(response.bodyAsText);
}
makeAuthenticatedRequest();
Other packages similar to @azure/core-http
axios
Axios is a popular HTTP client for making requests in both Node.js and the browser. It provides a simple and easy-to-use API for making HTTP requests, handling responses, and managing interceptors. Compared to @azure/core-http, Axios is more general-purpose and widely used outside of the Azure ecosystem.
node-fetch
Node-fetch is a lightweight module that brings the Fetch API to Node.js. It is designed to be a minimalistic and straightforward way to make HTTP requests. Compared to @azure/core-http, node-fetch is simpler and more focused on providing a fetch-like experience in Node.js.
request
Request is a comprehensive and flexible HTTP client for Node.js. It supports a wide range of features, including streaming, form data, and OAuth. While request is more feature-rich, it is also more complex and has been deprecated in favor of more modern alternatives like Axios and node-fetch.
Azure Core HTTP client library for JS
This is the core HTTP pipeline for Azure SDK JavaScript libraries which work in the browser and Node.js. This library is primarily intended to be used in code generated by AutoRest and autorest.typescript
.
Getting started
Requirements
- Node.js version > 6.x
- npm install -g typescript
Installation
- After cloning the repo, execute
npm install
Key concepts
You can find an explanation of how this repository's code works by going to our architecture overview.
Examples
Examples can be found in the samples
folder.
Next steps
Node.js
- Set the subscriptionId and token
- Run
node samples/node-sample.js
In the browser
- Set the subscriptionId and token and then run
- Open index.html file in the browser. It should show the response from GET request on the storage account. From Chrome type Ctrl + Shift + I and you can see the logs in console.
Troubleshooting
If you run into issues while using this library, please feel free to file an issue.
Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.microsoft.com.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.
If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.
This project has adopted the Microsoft Open Source Code of Conduct.
For more information see the Code of Conduct FAQ or
contact opencode@microsoft.com with any additional questions or comments.