Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 281
ElyashivLavi Avatar asked Sep 17 '15 07:09

ElyashivLavi


People also ask

How do you send multiple requests in cURL?

The solution to this is to use the xargs command as shown alongside the curl command. The -P flag denotes the number of requests in parallel. The section <(printf '%s\n' {1.. 10}) prints out the numbers 1 – 10 and causes the curl command to run 10 times with 5 requests running in parallel.

How do I use the cURL command to request?

To make a GET request using Curl, run the curl command followed by the target URL. Curl automatically selects the HTTP GET request method unless you use the -X, --request, or -d command-line option. The target URL is passed as the first command-line option.


1 Answers

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";
}
like image 97
ElyashivLavi Avatar answered Sep 28 '22 05:09

ElyashivLavi