Skip to content

Instantly share code, notes, and snippets.

@freestrings
Created December 18, 2015 05:53
Show Gist options
  • Save freestrings/119ce3af72447db57834 to your computer and use it in GitHub Desktop.
Save freestrings/119ce3af72447db57834 to your computer and use it in GitHub Desktop.

CURL fail option

$ curl -f "http://localhost/force_error.php" > /dev/null 2>&1 && echo Success || echo Fail
$ Fail

$ curl "http://localhost/force_error.php" > /dev/null 2>&1 && echo Success || echo Fail
$ Success 
<?php
http_response_code(500);
exit(1);
public class TestCurlProcess {
public static void main(String... args) throws Exception {
System.out.println("'-f' option exit code : " + execute("curl -f http://www.404.site")); // 6
System.out.println("no option exit code : " + execute("curl http://www.404.site")); // 6
System.out.println("-f option exit code : " + execute("curl -f http://localhost/force_error.php")); // 22
System.out.println("no option exit code : " + execute("curl http://localhost/force_error.php")); // 0
}
private static Integer execute(String cmd) throws Exception {
Process process = Runtime.getRuntime().exec(cmd, null);
Integer exitCode;
while (true) {
if (!process.isAlive()) {
exitCode = process.exitValue();
break;
}
Thread.sleep(100);
}
return exitCode;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment