Extract files from HAR
mouse 21 · person cloud · link
Last update
2020-03-09
2020
03-09
«HAR = HTTP Archive»
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env ruby
$VERBOSE = nil

require 'json'
require 'base64'

def die(msg, code=1); puts msg; exit code; end

die "File [#{ARGV[0] || '--'}] not found!" unless File.exist?(ARGV[0].to_s)

har = JSON.parse File.read ARGV[0] rescue nil
die "Non a JSON file!" unless har.is_a?(Hash)

har['log']['entries'].each do |e|
  begin
    url   = e['request']['url']
    fname = URI.parse(url).path[1..-1].tr('/', '_')

    content = e['response']['content']
    data = content['text']
    data = Base64.decode64 data if content['encoding'] == 'base64'

    File.open(fname, 'wb'){|f| f.write data }
  rescue
    puts "ERR: #{url}\n#{$!}\n\n"
  end
end

See also: har-extractor