Skip to content

sdstoehr/har-reader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HAR Reader

A Java library for reading and writing HTTP Archive (HAR) files.

Build Status codecov Maven Central License: MIT

Features

  • ✅ Read HAR files from File, String, or InputStream
  • ✅ Write HAR data to File, OutputStream, Writer, or byte array
  • ✅ Fluent builder API for creating HAR data structures
  • ✅ Support for non-standard date formats (LAX mode)
  • ✅ Customizable Jackson ObjectMapper configuration
  • ✅ Support for additional non-standard HAR fields
  • ✅ Fully compliant with HAR 1.2 specification
  • ✅ Records-based immutable model (Java 17+)

Table of Contents

Requirements

  • Java 17 or higher
  • Jackson 3.x (for version 4.0.0+)
  • Jackson 2.x (for version 3.1.6 and earlier)

Installation

Add the dependency to your pom.xml:

<dependency>
  <groupId>de.sstoehr</groupId>
  <artifactId>har-reader</artifactId>
  <version>4.0.1</version>
</dependency>

Usage

Reading HAR Files

From File

HarReader harReader = new HarReader();
Har har = harReader.readFromFile(new File("myhar.har"));
System.out.println(har.log().creator().name());

From String

HarReader harReader = new HarReader();
Har har = harReader.readFromString("{ ... HAR-JSON-Data ... }");

LAX Mode for Non-Standard Formats

Some HAR generators use date formats that don't comply with the specification. Use LAX mode to handle these gracefully:

HarReader harReader = new HarReader();
Har har = harReader.readFromFile(new File("myhar.har"), HarReaderMode.LAX);
// or
Har har = harReader.readFromString("{ ... }", HarReaderMode.LAX);

For more control over date parsing, see the Customization section.

Writing HAR Files

To File

Har har = new Har();
HarWriter harWriter = new HarWriter();
harWriter.writeTo(new File("myhar.har"), har);

To OutputStream

Har har = new Har();
HarWriter harWriter = new HarWriter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
harWriter.writeTo(baos, har);

To Writer

Har har = new Har();
HarWriter harWriter = new HarWriter();
StringWriter sw = new StringWriter();
harWriter.writeTo(sw, har);

As Byte Array

Har har = new Har();
HarWriter harWriter = new HarWriter();
byte[] harBytes = harWriter.writeAsBytes(har);

Building HAR Data

Create HAR data structures using the fluent builder API:

Har har = Har.builder()
    .log(HarLog.builder()
        .creator(HarCreatorBrowser.builder()
            .name("HAR reader")
            .version("1.0")
            .build())
        .entry(HarEntry.builder()
            .pageref("page_0")
            .startedDateTime(ZonedDateTime.parse("2021-01-01T00:00:00Z"))
            .time(42)
            .request(HarRequest.builder()
                .method(HttpMethod.GET.name())
                .url("https://www.example.com")
                .httpVersion("HTTP/1.1")
                .build())
            .response(HarResponse.builder()
                .status(200)
                .statusText("OK")
                .httpVersion("HTTP/1.1")
                .build())
            .build())
        .build())
    .build();

Builder API Features

The builders support both single and batch operations:

// Add single entry
harLogBuilder.page(HarPage page);

// Add multiple entries (does NOT replace existing entries)
harLogBuilder.pages(List<HarPage> pages);

// Clear all entries
harLogBuilder.clearPages();

Updating Existing Objects

Use .toBuilder() to create a modified copy of an existing object:

Har updatedHar = har.toBuilder()
    .log(har.log().toBuilder()
        .comment("Updated comment")
        .build())
    .build();

Customization

Create a custom MapperFactory to configure Jackson's ObjectMapper behavior:

public class MyMapperFactory implements MapperFactory {
    public ObjectMapper instance(HarReaderMode mode) {
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();

        // Configure Jackson object mapper as needed
        // e.g., custom date deserializers, property naming strategies, etc.

        mapper.registerModule(module);
        return mapper;
    }
}

Use your custom factory:

HarReader harReader = new HarReader(new MyMapperFactory());

See DefaultMapperFactory for reference.

Release Notes

4.0.1 - 2025-11-16

  • Dependency updates

Full Release Details

4.0.0 - 2025-10-05

⚠️ BREAKING CHANGE: Switched to Jackson 3

If you need Jackson 2 support, use version 3.1.6.

Full Release Details

3.1.6 - 2025-10-02

  • Dependency updates

Full Release Details

3.1.0 - 2025-04-23

⚠️ BREAKING CHANGE: Switched timings from int to long

Full Release Details

3.0.1 - 2024-12-21

Major modernization release:

  • Minimum Java version: 17
  • Records-based model (immutable by default)
  • ZonedDateTime instead of Date
  • Proper @Nullable and @NotNull annotations

Full Release Details & Breaking Changes

View Older Releases

2.x Series (Java 8+)

  • 2.5.0 (2024-11-20): Fixed browser field nullability
  • 2.4.1 (2024-11-15): Fixed duplicate fields issue
  • 2.4.0 (2024-11-13): HAR serialization support, unknown HTTP methods/status codes
  • 2.3.0 (2023-11-17): Dropped Java 7 support
  • 2.2.1 (2022-05-26): Default values compliance
  • 2.2.0 (2021-03-27): Support for additional non-standard fields
  • 2.1.x (2018-2020): Various dependency updates and minor enhancements
  • 2.0.0 (2015-08-30): Customizable MapperFactory support

View All Releases

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

Library for accessing HTTP Archives (HAR) with Java

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 11

Languages