httpyac CLI Client
httpYac (current version 6.16.7) is a robust command-line interface (CLI) and Node.js package designed for testing and automating API interactions using `.http` and `.rest` files. It supports an extensive range of protocols including HTTP, REST, GraphQL, WebSocket, gRPC, RabbitMQ (AMQP), EventSource, and MQTT, offering a versatile solution beyond traditional HTTP-only clients. The project maintains an active development pace, with frequent minor releases (e.g., 6.16.x) primarily focusing on bug fixes, performance improvements, and feature enhancements, as demonstrated by its detailed changelog entries. Its core strength lies in enabling developers to define complex request sequences, variables, and test assertions directly within `.http` files, providing a file-based alternative to GUI tools like Postman or Insomnia. A key differentiator is its extensibility and the fact that it ships with comprehensive TypeScript types, facilitating programmatic integration despite its primary CLI orientation. It requires Node.js version 18 or higher to run effectively, ensuring compatibility with modern JavaScript runtime features. The tool also boasts a popular VS Code extension, making it a powerful choice for developers working within that ecosystem.
Common errors
-
httpyac: command not found
cause The `httpyac` CLI tool is not installed globally on your system, or its installation directory is not included in your system's PATH environment variable.fixInstall `httpyac` globally using `npm install -g httpyac` (or `yarn global add httpyac`). Verify that your npm/yarn global bin directory is correctly added to your system's PATH. -
Error: Cannot find module 'httpyac'
cause You are attempting to `import` or `require` the `httpyac` package programmatically, but it has not been installed as a local dependency in your project.fixInstall `httpyac` as a local project dependency: `npm install httpyac` or `yarn add httpyac`. -
Error: Invalid HTTP file syntax at line X: ...
cause The `.http` or `.rest` file you are trying to execute contains a syntax error, such as a malformed request line, incorrect header format, or an improperly defined variable or comment.fixReview the specified line number and the surrounding content in your `.http` file. Consult the `httpyac` documentation (or the IntelliJ HTTP Client specification which it largely follows) for correct syntax rules. -
SSL Handshake failed: self-signed certificate in certificate chain
cause The target server uses an SSL certificate that is not trusted by your system's (or Node.js's) default Certificate Authorities, typically a self-signed certificate or one from an internal CA.fixFor development or testing purposes, you can use the `--insecure` flag with the CLI (`httpyac send --insecure ...`) or set `insecure: true` in programmatic options. For production or secure environments, ensure the server has a valid, publicly trusted SSL certificate, or configure Node.js to explicitly trust your custom CA certificate. -
OAuth2: Invalid state error with authorization code flow
cause During an OAuth2 authorization code flow, the `state` parameter returned by the authorization server does not match the original `state` parameter generated by `httpyac`, potentially due to an encoding issue or a mismatch in the flow.fixThis specific issue (when the state parameter is percent-encoded) was fixed in `httpyac v6.16.7`. Ensure you are using `httpyac@6.16.7` or a newer version. If the problem persists, review your OAuth2 provider's configuration and ensure callback URLs are correctly configured and match.
Warnings
- breaking As of `v6.16.3`, the behavior of `@ref` (referenced HTTP regions) has changed significantly. Negative test results from a reference request no longer automatically stop the dependent request, and errored references are not executed multiple times if referenced repeatedly. This alters the flow of complex test suites.
- breaking Since `v6.16.2`, named response objects, which are often used to propagate variables, are only populated if *all* associated test results for that request are valid. Previously, named responses might have been available even with some test failures, which could lead to unexpected behavior in scripts relying on those variables.
- gotcha While `httpyac` offers programmatic access and ships TypeScript types, its primary design and documentation focus is heavily on its command-line interface and VS Code extension. Developers seeking deep programmatic integration might encounter fewer high-level abstractions or dedicated library-specific guides compared to other packages.
- gotcha Using the `--insecure` CLI flag (or `insecure: true` in programmatic options) bypasses SSL certificate validation. This can expose connections to man-in-the-middle attacks and is generally not recommended for production environments or sensitive data.
- gotcha `httpyac` supports multiple mechanisms for defining variables and environments (e.g., `-e`, `--var`, `$env` placeholders, `.env` files, and `oauth2_proxy`). Understanding the precedence rules can be challenging, leading to unexpected variable values if not managed carefully.
Install
-
npm install httpyac -
yarn add httpyac -
pnpm add httpyac
Imports
- executeFile
const executeFile = require('httpyac')import { executeFile } from 'httpyac' - HttpYacConfig
import { HttpYacConfig } from 'httpyac'import type { HttpYacConfig } from 'httpyac' - processor
import { Processor } from 'httpyac'import { processor } from 'httpyac'
Quickstart
// requests.http (e.g., in a file named 'example.http')
@user = testuser
@password = secretpassword
// A basic GET request to a public API endpoint
GET https://httpbin.org/basic-auth/{{user}}/{{password}}
Authorization: Basic {{user}} {{password}}
Accept: application/json
###
// A POST request with a JSON body and an environment variable
@apiBaseUrl = https://httpbin.org
POST {{apiBaseUrl}}/post
Content-Type: application/json
{
"message": "Hello from httpyac!",
"timestamp": "{{$isoTimestamp}}",
"env_data": "{{$env.MY_APP_DATA ?? 'default'}}"
}
// JavaScript/TypeScript programmatic runner (e.g., 'run.ts')
import { executeFile } from 'httpyac';
import path from 'path';
async function runHttpYacFile() {
// Path to your .http file
const filePath = path.resolve(__dirname, './example.http');
// Set an environment variable for the run (optional)
process.env.MY_APP_DATA = 'important_value';
try {
const results = await executeFile(filePath, {
// Options for execution
log: console, // Use console for logging output
all: true, // Execute all requests in the file
// Pass environment variables specific to this run
environments: {
development: { // An environment named 'development'
token: 'my-auth-token'
}
},
// Optionally specify a subset of requests by name or line number
// name: 'POST request with a JSON body',
// line: 10 // Line number of the request start
});
console.log('\n--- Execution Summary ---');
results.forEach((exchange, index) => {
console.log(`Request ${index + 1}: ${exchange.request.url}`);
console.log(` Status: ${exchange.response?.statusCode} ${exchange.response?.statusMessage}`);
if (exchange.response?.body) {
console.log(` Body: ${exchange.response.body.toString().substring(0, 100)}...`);
}
if (exchange.testResults?.length) {
console.log(' Tests:');
exchange.testResults.forEach(test => console.log(` - ${test.name}: ${test.result ? 'Passed' : 'Failed'}`));
}
if (exchange.error) {
console.error(` Error: ${exchange.error.message}`);
}
});
} catch (error) {
console.error('An unexpected error occurred during execution:', error);
}
}
runHttpYacFile();