Debug Ajax Requests with the Browsers Copy as cURL Feature

Debugging Ajax requests can be annoying, especially when you are sending a lot of data over like serializing an entire form. Chrome, Opera, Safari, Firefox, and other browsers all now include the ability to copy an Ajax request as cURL. This is really useful for debugging and testing while you are working locally.

Here is how you can copy the cURL request from two popular browsers:

Once you’ve copied it, your clipboard will contain a string like this with everything that is sent in the request

curl 'http://laravel-5.5.dev/api/test' -H 'Pragma: no-cache' -H 'Origin: http://laravel-5.5.dev' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Accept: */*' -H 'Cache-Control: no-cache' -H 'X-Requested-With: XMLHttpRequest' -H 'Cookie: XSRF-TOKEN=eyJpdiI6IlZIQWFLVEpmcys1M0pRNDE4N2FrQnc9PSIsInZhbHVlIjoiZ205N3haN0htMHU2b05wcmdQVGk4cmZYS1hqaENvXC9GVGdqWkZGZTRjQ1Z5MVhVU0NBWXhCSng5QWtzNVNlUTIydDJlRGlONmErQzk4VHpDVWc4cWVRPT0iLCJtYWMiOiJmNWY0NTJmYWI1ZDMxNWIzNjY0OTQ2YjlmNzU4NGM3MWY4NWFlNjAzMWU0NWI5Yjk5NmE5ZDUzZWI2YTE4Zjc5In0%3D; laravel_session=eyJpdiI6IlpmSGptSENFSXc5SmEzTmdmQitvc2c9PSIsInZhbHVlIjoiT1dZcld0eldKTXpIMjVUMUJaWHYySWswd2djSGR0SEFmZnhoTDNxbGJYU1BWZU9DTVlaK1A1UFwvaHlSVXdmOTZicmM5cTFnMjd5TEJveUk3UWJSTnZ3PT0iLCJtYWMiOiIzYzQ3ODU4YTZkNDAzYTg5NDQ1MDBmYTE4YmZjNjE5NzNmYTQxZDk0OGVhZDkxNjBkODI4NTRiZWU1ZWZkM2EyIn0%3D' -H 'Connection: keep-alive' -H 'Referer: http://laravel-5.5.dev/' -H 'DNT: 1' --data 'name=John&email=test%40test.com&age=67' --compressed

This allows you to paste it into a terminal and quickly run it again, or paste it into a text editor and make changes on the fly. The only downside is with encrypted strings it gets messy.

So you can copy and paste it into a service like curl-to-php and it’ll convert into PHP compatible curl code:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://laravel-5.5.dev/api/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John&email=test%40test.com&age=67");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

$headers = array();
$headers[] = "Pragma: no-cache";
$headers[] = "Origin: http://laravel-5.5.dev";
$headers[] = "Accept-Encoding: gzip, deflate";
$headers[] = "Accept-Language: en-US,en;q=0.8";
$headers[] = "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
$headers[] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";
$headers[] = "Accept: */*";
$headers[] = "Cache-Control: no-cache";
$headers[] = "X-Requested-With: XMLHttpRequest";
$headers[] = "Cookie: XSRF-TOKEN=eyJpdiI6IlZIQWFLVEpmcys1M0pRNDE4N2FrQnc9PSIsInZhbHVlIjoiZ205N3haN0htMHU2b05wcmdQVGk4cmZYS1hqaENvXC9GVGdqWkZGZTRjQ1Z5MVhVU0NBWXhCSng5QWtzNVNlUTIydDJlRGlONmErQzk4VHpDVWc4cWVRPT0iLCJtYWMiOiJmNWY0NTJmYWI1ZDMxNWIzNjY0OTQ2YjlmNzU4NGM3MWY4NWFlNjAzMWU0NWI5Yjk5NmE5ZDUzZWI2YTE4Zjc5In0%3D; laravel_session=eyJpdiI6IlpmSGptSENFSXc5SmEzTmdmQitvc2c9PSIsInZhbHVlIjoiT1dZcld0eldKTXpIMjVUMUJaWHYySWswd2djSGR0SEFmZnhoTDNxbGJYU1BWZU9DTVlaK1A1UFwvaHlSVXdmOTZicmM5cTFnMjd5TEJveUk3UWJSTnZ3PT0iLCJtYWMiOiIzYzQ3ODU4YTZkNDAzYTg5NDQ1MDBmYTE4YmZjNjE5NzNmYTQxZDk0OGVhZDkxNjBkODI4NTRiZWU1ZWZkM2EyIn0%3D";
$headers[] = "Connection: keep-alive";
$headers[] = "Referer: http://laravel-5.5.dev/";
$headers[] = "Dnt: 1";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

I’ve found these two tools infinitely useful and I’d love to see a service like that curl-to-php that converts a cURL request into Guzzle compatible code.