Java
Chilkat2 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.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Rest;
import com.chilkatsoft.chilkat2.StringBuilder;
public class ChilkatExample {
public static void main(String[] args) {
Rest rest = new Rest();
boolean bTls = true;
boolean bAutoReconnect = true;
try {
rest.connect("example.com", 443, bTls, bAutoReconnect);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
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.
StringBuilder sbRequest = new StringBuilder();
sbRequest.append("{ \"query\": \"chilkat\" }");
rest.addHeader("Content-Type", "application/json");
StringBuilder sbResponse = new StringBuilder();
String responseText;
try {
rest.fullRequestSb("POST", "/api/search", sbRequest, sbResponse);
responseText = sbResponse.getAsString();
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println(responseText);
}
}