Skip to main content

Compressed Responses

The Slingshot API supports the compression of JSON / XML responses. To use response compression, set the standard http header Accept-Encoding to either gzip or deflate and the Slingshot API server will compress the data before sending it to the client.

Requesting

The following C# code sample shows how to set the Accept-Encoding header to ask the Slingshot API for compressed responses.

HttpWebRequest httpWebRequest;
httpWebRequest =
HttpWebRequest.Create("https://api.ravenslingshot.com/FieldHubs")
as HttpWebRequest;
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");

Reading

The following code samples demonstrate how to check for a compressed response and decompress it.

C#

This sample dumps the response to the console. Change the Decompress.CopyTo target to the preferred stream.

string contentEncoding = resp.Headers["Content-Encoding"];
if (String.IsNullOrEmpty(contentEncoding))
{
Console.WriteLine(resp.Content.ReadAsString());
}
else
{
if (contentEncoding.ToUpperInvariant().Contains("GZIP"))
{
using (GZipStream Decompress =
new GZipStream(resp.Content.ReadAsStream(),
CompressionMode.Decompress))
{
// Copy the decompression stream
// into the output file.
Decompress.CopyTo(Console.OpenStandardOutput());
}
Console.WriteLine();
}
else if (contentEncoding.ToUpperInvariant().Contains("DEFLATE"))
{
using (DeflateStream Decompress =
new DeflateStream(resp.Content.ReadAsStream(),
CompressionMode.Decompress))
{
// Copy the decompression stream
// into the output file.
Decompress.CopyTo(Console.OpenStandardOutput());
}
Console.WriteLine();
}
else
{
Console.WriteLine("compression method {0} not supported", contentEncoding);
}
}

PHP

This sample dumps the response to the console.

//initialize curl
$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, "https://api.ravenslingshot.com/FieldHubs");

curl_setopt($curl, CURLOPT_HEADER, false);//false - truncates the header from the response (if header set to 'true' is desired)
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//prepare header
$header = array();
array_push($header, 'X-SS-APIKey: '. 'yourApiKey');
array_push($header, 'X-SS-AccessKey: '. 'yourAccessKey');
array_push($header, 'X-SS-TimeStamp: '. 'yourTimestamp');
array_push($header, 'X-SS-Signature: '.'yourSignature');

//request for compressed reponse
array_push($header, 'Accept-Encoding: gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

//make the call
$curl_response = curl_exec($curl);

//uncompress the data
$uncompressed = gzdecode($curl_response);

//pretty print
echo "<pre>".htmlentities($uncompressed)."</pre>";

//close curl connection
curl_close($curl);