Using HTTP Compression
The EDS API supports HTTP compression. Compressed responses will be returned for requests indicating that a compressed response is desired. Compressed data is smaller and will create smaller response messages and decrease response time.
You can use many different client programming languages with the EDS API. In order to utilize HTTP compression, each programming language has a way to set flags in the http header to indicate that the client accepts encoded responses. Generally, it is recommended practice for performance to have Accept-Encoding: gzip, deflate present in the http header. See the Perl, PHP, Java and Ruby example code snippets below for more detailed information on how to set the HTTP header for compression.
my ($method, $uri, $body, $auth, $sess) = @_;
my $req = HTTP::Request->new( $method, $uri );
$req->header( 'Content->Type' => 'application/json' );
# Set the header for compression$req->header( 'Accept-Encoding' => 'gzip, deflate' );$req->header( 'x-authenticationToken' => $auth );
$req->header( 'x-sessionToken' => $sess );
$req->content( $body );
my $lwp = LWP::UserAgent-?new;
my $response = $lwp->request( $req );
return $response->decoded_content(charset => 'none');
Option 1:
// Set the header for compression
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
Option 2:
// Set the header for compression
$headers = array_merge(
array('Accept-Encoding' => 'gzip,deflate'), $headers);
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers)}
public class ClientGZipContentCompression {
public final static void main(String[] args) throws Exception {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
// Set the header for compression if (!request.containsHeader("Accept-Encoding")) { request.addHeader("Accept-Encoding", "gzip"); } }
});
httpclient.addResponseInterceptor(new HttpResponseInterceptor() {
public void process(
final HttpResponse response,
final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(
new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
});
HttpGet httpget = new HttpGet("http://www.apache.org/"");
// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
# Set the header for compressionreq = Net::HTTP::Get.new( uri, { "Accept-Encoding" => "gzip", "User-Agent" => "gzip" } )res = http.request( req )
case res
when Net::HTTPSuccess then
begin
if res.header[ 'Content-Encoding' ].eql?( 'gzip' ) then
debug.call( "Performing gzip decompression for response body." )
sio = StringIO.new( res.body )
gz = Zlib::GzipReader.new( sio )
page = gz.read()
debug.call( "Finished decompressing gzipped response body." )
Updated 10 months ago