What is @azure/ms-rest-azure-js?
@azure/ms-rest-azure-js is a library that provides Azure-specific implementations of the ms-rest-js interfaces. It is used to handle authentication, HTTP requests, and responses when interacting with Azure services.
What are @azure/ms-rest-azure-js's main functionalities?
Authentication
This feature allows you to authenticate with Azure using a service principal and secret. The code sample demonstrates how to use the `loginWithServicePrincipalSecret` method to obtain credentials.
const msRestAzure = require('@azure/ms-rest-azure-js');
const loginWithServicePrincipalSecret = msRestAzure.loginWithServicePrincipalSecret;
const clientId = 'your-client-id';
const secret = 'your-secret';
const domain = 'your-tenant-id';
loginWithServicePrincipalSecret(clientId, secret, domain).then((credentials) => {
console.log('Authenticated successfully');
}).catch((err) => {
console.error('Authentication failed', err);
});
HTTP Requests
This feature allows you to make authenticated HTTP requests to Azure services. The code sample demonstrates how to create and sign a request using the obtained credentials.
const msRestAzure = require('@azure/ms-rest-azure-js');
const loginWithServicePrincipalSecret = msRestAzure.loginWithServicePrincipalSecret;
const { WebResource } = require('@azure/ms-rest-js');
const clientId = 'your-client-id';
const secret = 'your-secret';
const domain = 'your-tenant-id';
loginWithServicePrincipalSecret(clientId, secret, domain).then((credentials) => {
const request = new WebResource();
request.url = 'https://management.azure.com/subscriptions?api-version=2020-01-01';
request.method = 'GET';
credentials.signRequest(request).then((signedRequest) => {
console.log('Signed request:', signedRequest);
});
}).catch((err) => {
console.error('Authentication failed', err);
});
Handling Responses
This feature allows you to handle responses from Azure services. The code sample demonstrates how to send a signed request and handle the response, including status and body.
const msRestAzure = require('@azure/ms-rest-azure-js');
const loginWithServicePrincipalSecret = msRestAzure.loginWithServicePrincipalSecret;
const { WebResource, HttpOperationResponse } = require('@azure/ms-rest-js');
const clientId = 'your-client-id';
const secret = 'your-secret';
const domain = 'your-tenant-id';
loginWithServicePrincipalSecret(clientId, secret, domain).then((credentials) => {
const request = new WebResource();
request.url = 'https://management.azure.com/subscriptions?api-version=2020-01-01';
request.method = 'GET';
credentials.signRequest(request).then((signedRequest) => {
signedRequest.send().then((response) => {
console.log('Response status:', response.status);
console.log('Response body:', response.bodyAsText);
});
});
}).catch((err) => {
console.error('Authentication failed', err);
});
Other packages similar to @azure/ms-rest-azure-js
@azure/identity
@azure/identity provides a unified way to authenticate with Azure services. It supports various authentication methods, including managed identities, service principals, and interactive browser authentication. Compared to @azure/ms-rest-azure-js, it offers more modern and flexible authentication options.
@azure/ms-rest-js
@azure/ms-rest-js is a core library for handling HTTP requests and responses. It provides the foundational components used by @azure/ms-rest-azure-js but does not include Azure-specific authentication mechanisms. It is more general-purpose and can be used for any HTTP-based service.
@azure/core-http
@azure/core-http is part of the Azure SDK for JavaScript and provides HTTP client functionality. It is designed to be used with the new Azure SDK libraries and offers improved performance and usability compared to @azure/ms-rest-azure-js.
ms-rest-azure-js
Azure Runtime with support for long running operations used by packages generated via the code generator @microsoft.azure/autorest.typescript.
Use this package if you are writing a library for one of the Azure services whose API requires support for long running operations.
To write a library for one of the Azure services whose API does not need support for long running operations, @azure/ms-rest-js should suffice.
If you are not a library writer for any of the Azure services, you should not need to use this package except for one scenario which is authenticating to any of the Cognitive Services using the below packages. This is when you would use the CognitiveServicesCredentials exported from this package.
Requirements
- node.js version > 6.x
- npm install -g typescript
Installation
- After cloning the repo, execute
npm install
Execution
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.
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.
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.