curl License: Unlicense

curl command in java (using Apache libs : HttpClient and commons-cli)

Setup with maven







    org.apache.http.HttpResponse org.toilelibre.libe.curl.Curl.curl (String curlParams);
    String org.toilelibre.libe.curl.Curl.$ (String curlCommand); //Returns responseBody

You can import static these methods :

    import static org.toilelibre.libe.curl.Curl.curl;
    import static org.toilelibre.libe.curl.Curl.$;

Examples :

    $("curl https://localhost:8443/public/");
    $("curl -k https://localhost:8443/public/");
    curl("-k https://localhost:8443/public/");
    curl("-k --cert src/test/resources/client.p12:password https://localhost:8443/public/");
    curl("-k https://localhost:8443/public/redirection");
    curl("-k https://localhost:8443/public/unauthorized");
    curl("-k -L https://localhost:8443/public/redirection");
    curl("-k -H'Host: localhost' -H'Authorization: 00000000-0000-0000-0000-000000000000' https://localhost:8443/public/v1/coverage/sncf/journeys?from=admin:7444extern");
    curl("-k -X GET -H 'User-Agent: curl/7.49.1' -H 'Accept: */*' -H 'Host: localhost'  'https://localhost:8443/public/curlCommand1?param1=value1&param2=value2'");
    curl("-k -X GET -H 'User-Agent: curl/7.49.1' -H 'Accept: */*' -H 'Host: localhost' -u foo:bar 'https://localhost:8443/private/login'");
    curl("-L -k -X GET -H 'User-Agent: curl/7.49.1' -H 'Accept: */*' -H 'Host: localhost' -u user:password 'https://localhost:8443/private/login'");
    curl("-k -X POST 'https://localhost:8443/public/json' -d '{\"var1\":\"val1\",\"var2\":\"val2\"}'");

It also works with a builder

    HttpResponse response = curl().k().xUpperCase("POST").d("{\"var1\":\"val1\",\"var2\":\"val2\"}").run("https://localhost:8443/public/json");

How to get Google Homepage with this lib :

    public String getGoogleHomepage (){
        //-L is passed to follow the redirects
        return curl ().lUpperCase ().$ ("");

You can also specify three additional curl options using jvm code :

   .javaOptions(with().interceptor(((request, responseSupplier) -> {"I log something before the call");
       HttpResponse response = responseSupplier.get();"I log something after the call, status code is {}",
       return response;}))
                      .connectionManager(new PoolingHttpClientConnectionManager ())
                      .placeHolders(asList("fr-FR", "text/html")).build())
   .hUpperCase("'Accept-Language: $curl_placeholder_0'")
   .hUpperCase("'Accept: $curl_placeholder_1'")

Supported arguments (so far) :

Short Name Long Name Argument Required Description
u username true user:password
cacert cacert true CA_CERT
E cert true CERT[:password]
ct cert-type true PEM,P12,JKS,DER,ENG
compressed compressed false Request compressed response
cti connect-timeout true Maximum time allowed for connection
d data true Data
databinary data-binary true http post binary data
dataurlencode data-urlencode true Data to URLEncode
L location false follow redirects
F form true http multipart post data
H header true Header
X request true Http Method
key key true KEY
kt key-type true PEM,P12,JKS,DER,ENG
m max-time true Maximum time allowed for the transfer
nokeepalive no-keepalive false Disable TCP keepalive on the connection
ntlm ntlm false NTLM auth
o output true write to file
x proxy true use the specified HTTP proxy
U proxy-user true authentication for proxy
1 tlsv1 false use >= TLSv1 (SSL)
tlsv10 tlsv1.0 false use TLSv1.0 (SSL)
tlsv11 tlsv1.1 false use TLSv1.1 (SSL)
tlsv12 tlsv1.2 false use TLSv1.2 (SSL)
2 sslv2 false use SSLv2 (SSL)
3 sslv3 false use SSLv3 (SSL)
k insecure false trust insecure
A user-agent true user agent
V version false get the version of this library
interceptor interceptor true interceptor field or method (syntax is classname::fieldname). Must be a BiFunction<HttpRequest, Supplier< HttpResponse>, HttpResponse> or will be discarded