Skip to content

Save your charles logs to .HAR - HTTP Archive format - playback in swift + Alamofire

Notifications You must be signed in to change notification settings

johndpope/HAR-replay-swift

Repository files navigation

Saving HAR HTTP-Archive files from Charles / Replay your API traffic - today or years from now

This library combines OHHTTPStubs + SwiftyJSON to provide a mock server in 1 line.
Let OHTTPStubs replay your traffic eg. openlab.har

Snapshot your API for playback later on / for unit tests or troubleshooting test captures.

usage HARStub.stubHarFile(fileName: "openlab")


// To remove stubs
 OHHTTPStubs.removeAllStubs()
 
 
example with Alamofire 

 let stubbedPayload: [Dictionary<String, String>] = HARStub.stubHarFile(fileName: "openlab")
        for d0 in stubbedPayload {
            // Go offline - and watch it replay!
            if let url = d0["url"] {
                print("url:",url)
                Alamofire.request(url).response { response in
                    print("Request: \(response.request)")
                    print("Response: \(response.response)")
                    print("Error: \(response.error)")
                    
                    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                        print("Data: \(utf8Text)")
                    }
                }
                
            }
        }

Consuming HAR files

The HAR files allow for analysis at a later time of the session(s) that have been recorded.

As to how to "visualise" the HAR files, below are some options:

HAR Viewer

Developed by the author of the [NetExport] extension to FireBug, it's the one I've found mentioned more often (and the one I tend to use):

Other tools

About

Save your charles logs to .HAR - HTTP Archive format - playback in swift + Alamofire

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published