phpgoogle-chromehttprequesthar

How to repeat Chrome requests as curl commands?


For some automated tests that I did, I had to record requests from Chrome, and then repeat them in curl commands. I start checking how to do it...


Solution

  • The way I did it was:

    1. Access websites when the developers tools open.
    2. Issue requests, make sure they are logged in the console.
    3. Right click on the requests, select 'Save as HAR with content', and save to a file.
    4. Then run the following php script to parse the HAR file and output the correct curls:

    script:

    <?php    
    $contents=file_get_contents('/home/elyashivl/har.har');
    $json = json_decode($contents);
    $entries = $json->log->entries;
    foreach ($entries as $entry) {
      $req = $entry->request;
      $curl = 'curl -X '.$req->method;
      foreach($req->headers as $header) {
        $curl .= " -H '$header->name: $header->value'";
      }
      if (property_exists($req, 'postData')) {
        # Json encode to convert newline to literal '\n'
        $data = json_encode((string)$req->postData->text);
        $curl .= " -d '$data'";
      }
      $curl .= " '$req->url'";
      echo $curl."\n";
    }