What is @azure-rest/core-client?
@azure-rest/core-client is a core library for building Azure SDKs using the REST client. It provides essential functionalities for making HTTP requests, handling responses, and managing client configurations. This package is designed to be used as a foundational component for other Azure SDK libraries.
What are @azure-rest/core-client's main functionalities?
Creating a REST Client
This feature allows you to create a REST client instance that can be used to interact with Azure services. The `createClient` function initializes the client with a base URL.
const { createClient } = require('@azure-rest/core-client');
const client = createClient('https://example.azure.com');
Making GET Requests
This feature demonstrates how to make a GET request to a specified path using the REST client. The response body is then logged to the console.
const response = await client.path('/resource').get();
console.log(response.body);
Making POST Requests
This feature shows how to make a POST request with a JSON body. The response body is then logged to the console.
const response = await client.path('/resource').post({ body: { key: 'value' } });
console.log(response.body);
Handling Responses
This feature demonstrates how to handle responses from the REST client. It checks the status code and logs either a success message with the response body or an error message with the status code.
const response = await client.path('/resource').get();
if (response.status === 200) {
console.log('Success:', response.body);
} else {
console.log('Error:', response.status);
}
Configuring Client Options
This feature shows how to configure client options such as headers when creating a REST client instance. In this example, an Authorization header is added to the client.
const { createClient } = require('@azure-rest/core-client');
const client = createClient('https://example.azure.com', { headers: { 'Authorization': 'Bearer token' } });
Other packages similar to @azure-rest/core-client
axios
Axios is a popular promise-based HTTP client for the browser and Node.js. It provides similar functionalities for making HTTP requests and handling responses. Compared to @azure-rest/core-client, Axios is more general-purpose and not specifically tailored for Azure services.
node-fetch
Node-fetch is a lightweight module that brings `window.fetch` to Node.js. It is used for making HTTP requests and handling responses. While it offers similar functionalities, it lacks the built-in Azure-specific configurations and utilities provided by @azure-rest/core-client.
request
Request is a simplified HTTP client for Node.js with support for various HTTP methods and options. It is similar to @azure-rest/core-client in terms of making HTTP requests, but it is not specifically designed for Azure SDKs and is now deprecated in favor of more modern alternatives.
Azure Rest Core client library for JavaScript
This library is primarily intended to be used in code generated by AutoRest and autorest.typescript
. Specifically for rest level clients
Getting started
Requirements
Currently supported environments
See our support policy for more details.
Installation
This package is primarily used in generated code and not meant to be consumed directly by end users.
Key concepts
Examples
Examples can be found in the samples
folder.
Next steps
You can build and run the tests locally by executing rushx test
. Explore the test
folder to see advanced usage and behavior of the public classes.
Learn more about AutoRest and the autorest.typescript extension for generating a compatible client on top of this package.
Troubleshooting
If you run into issues while using this library, please feel free to file an issue.
Contributing
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.