Search code examples
jsonjqhar

Parsing HAR file to get a table of HTTP header values


I am parsing a HAR file (chrome devtools generated) using jq. My objective is to get a "table" (or csv output could be nice also) with a list of the values of specific HTTP headers returned by the server, per request URL.

To get a list of all request URLs, I can do:

cat har.json | jq '.log.entries[].request.url'

Now I would like, for each request, a column with all the URLs and the next columns with the corresponding Content-Encoding and Content-Type HTTP header values returned in the response/headers section.

I managed to get the HTTP headers values with the following command:

cat har.json | jq '.log.entries[].response.headers[] | select(.name=="Content-Encoding" or .name=="Content-Type") | .value'

Now I would like to mix the URL and the header values. How can I do that?

There is a difficulty here because the headers may not be returned in the same order by the HTTP server.


Solution

  • You could use a filter like this:

    [ "url", "content-type", "content-encoding" ],
    (.log.entries[] | [
        .request.url,
        ((.response.headers[] | select(.name == "Content-Type").value) // ""),
        ((.response.headers[] | select(.name == "Content-Encoding").value) // "")
    ])
    | @csv
    

    The key here is that for the content type and encoding, you need to perform a search for those headers separately if you want to control the order they appear. Then from there, you just need to format it in such a way it could be output as csv.