Response Body Handler

SaltwaterC edited this page Jan 29, 2012 · 6 revisions

The response body handler is used by all API calls that receive a HTTP response body. However, it is exposed into the s3.get() low level method in order to be able to use their full capabilities.

In order to enable specific behaviors of the response body handling, you need to pass as argument one of the following:

  • 'xml' - passing the 'xml' string instructs the internals of the AWS APIs clients to handle the response body as XML document. While this is the default behavior for the query APIs, for the S3 client it makes little sense, unless you're hosting XML documents on S3 itself or you're fetching S3 specific listings which are returned as XML documents (such as bucket listings). However, this handler is automatically turned on if one of the HTTP Errors is detected. Even the S3 API returns an XML body for a response with HTTP error code. For successful operations, the XML Parser output is returned into the callback response argument.
  • 'buffer' - passing the 'buffer' string instructs the internals of the AWS APIs clients to return the body of the HTTP response into the response argument of the callback. Basically the response argument object has a couple of properties: headers - which contains the response.headers object as returned by the node.js core HTTPS client; buffer - which contains the buffered response body as Buffer instance.
  • 'stream' - passing the 'stream' string instructs the internals of the AWS APIs client to return the HTTP response itself into the response argument of the passed callback. The HTTP response implements the Readable Stream interface, therefore you may use it as such. If the stream is returned when no errors are detected, then it's your responsibility to handle it properly.
  • null - passing the null value instructs the internals of the AWS APIs client to not handle at all the HTTP response body. This is useful for requests that don't have response bodies (such as HEAD). Using this handler by yourself doesn't make any sense since it's exposed for the GET method that normally should have a response body. This handler is implemented for the other library internals.
  • the file handler - if none of the above mentioned handlers are in use, then the library tries to use this handler. You must pass an object containing the file key which indicates the actual path. Example: {file: '/path/to/file.ext'}. If the operation is successful, then the response argument of the callback returns an object that contain the same structure as the input object of this handler. However, if you used a relative path as input for this handler, then the response object contains an absolute path as resolved by path.resolve().

If the library fails to identify the file handler, which is the default if the others aren't used, then an error is thrown.