Java
Java
Send a REST Request using StringBuilder Bodies
See more REST Examples
Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.
Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.
Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkRest rest = new CkRest();
boolean bTls = true;
boolean bAutoReconnect = true;
success = rest.Connect("example.com",443,bTls,bAutoReconnect);
if (success == false) {
System.out.println(rest.lastErrorText());
return;
}
// FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
// response body in a second StringBuilder. This avoids extra string conversions for large bodies.
CkStringBuilder sbRequest = new CkStringBuilder();
sbRequest.Append("{ \"query\": \"chilkat\" }");
rest.AddHeader("Content-Type","application/json");
CkStringBuilder sbResponse = new CkStringBuilder();
success = rest.FullRequestSb("POST","/api/search",sbRequest,sbResponse);
if (success == false) {
System.out.println(rest.lastErrorText());
return;
}
String responseText = sbResponse.getAsString();
if (sbResponse.get_LastMethodSuccess() == false) {
System.out.println(sbResponse.lastErrorText());
return;
}
System.out.println(responseText);
}
}