httpyac CLI Client

6.16.7 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic `.http` file with variables and multiple requests, and then execute it programmatically using the `httpyac` library in a TypeScript environment, logging the results.

// 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();

view raw JSON →