curl command in java (using Apache libs : HttpClient and commons-cli)
Setup with maven
<dependency>
<groupId>org.toile-libre.libe</groupId>
<artifactId>curl</artifactId>
</dependency>
Usage
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¶m2=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 ().$ ("https://www.google.com/");
}
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 |
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 |
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 |