In the previous section, you set up and made the web service request. The web service responds to your application's request with an XML message. The message contains the results of the operation you requested, or errors if the call did not succeed.
A typical response message has an
OperationRequest element, with information
about the entire request, and a results element such as
GetAccountBalanceResult, with the data
returned by the operation call.
<?xml version="1.0" encoding="UTF-8"?>
<GetAccountBalanceResponse xmlns="http://requester.mturk.amazonaws.com/doc/2006-10-31">
<OperationRequest>
<RequestId>
03T6GW17VJRGKQ50YPQ8
</RequestId>
</OperationRequest>
<GetAccountBalanceResult>
<Request>
<IsValid>
True
</IsValid>
</Request>
<AvailableBalance>
...
</AvailableBalance>
...
</GetAccountBalanceResult>
</GetAccountBalanceResponse>
The web service can return two kinds of errors: errors about your request, such as problems with the request's signature, and errors about the call to the operation, such as missing operation parameters.
Errors about the request appear in the
OperationRequest element. For example, if
the wrong secret key was used to calculate the
Signature, the response might include the
following error:
<OperationRequest>
<RequestId>
02FEKWBKDS5ZZ63Y4N5N
</RequestId>
<Errors>
<Error>
<Code>
AWS.NotAuthorized
</Code>
<Message>
The identity contained in the request is not authorized to use this AWSAccessKeyId
</Message>
</Error>
</Errors>
</OperationRequest>
Errors returned by the operation itself appear in the results
element. For example, if a call to the
ApproveAssignment operation does not include
an AssignmentId parameter, the response
might include the following error:
<ApproveAssignmentResult>
<Request>
<IsValid>
False
</IsValid>
<Errors>
<Error>
<Code>
AWS.MissingParameters
</Code>
<Message>
Your request is missing required parameters. Required parameters include AssignmentId.
</Message>
</Error>
</Errors>
</Request>
</ApproveAssignmentResult>
The next section demonstrates how to examine the response for errors and results, and print them to the screen.
The response object returned by the SOAP proxy object might be null if the client could not establish a connection. If not null, the response object represents the root element of the response, and child elements can be examined with accessor methods.
To understand the response and display results in Java
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
// Define other support routines
private static void printErrors( ErrorsError [] errorArray ) {
System.out.println( "There was an error processing your request:" );
for ( int i = 0; i < errorArray.length; i++ ) {
System.out.println(" Error code: " + errorArray[i].getCode());
System.out.println(" Error message: " + errorArray[i].getMessage());
}
}
In the main routine, check if the proxy class returned a null value.
// Check for and display results and errors
if ( response == null )
throw new Exception ( "Could not get response from service" );
Check if the service returned request-level errors and if so, display them.
OperationRequest operationRequest = response.getOperationRequest();
if ( operationRequest != null
&& operationRequest.getErrors() != null ) {
printErrors( operationRequest.getErrors() );
}
Check if the service returned operation-level errors and if so, display them. If not, get a value from the results and display it.
GetAccountBalanceResult [] getAccountBalanceResultArray = response.getGetAccountBalanceResult();
if ( getAccountBalanceResultArray != null ) {
if ( getAccountBalanceResultArray[0].getRequest().getErrors() != null ) {
printErrors( getAccountBalanceResultArray[0].getRequest().getErrors() );
} else {
System.out.println("Account balance: " + getAccountBalanceResultArray[0].getAvailableBalance().getFormattedPrice());
}
}
The response object returned by the SOAP proxy object might be null if the client could not establish a connection. If not null, the response object represents the root element of the response. You can examine child elements with accessor methods.
To understand the response and display results in C#
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
// Define other support routines
static void printErrors(ErrorsError[] errorArray)
{
Console.WriteLine("There was an error processing your request:");
for (int i = 0; i < errorArray.GetLength(0); i++)
{
Console.WriteLine(" Error code: " + errorArray[i].Code);
Console.WriteLine(" Error message: " + errorArray[i].Message);
}
}
In the main routine, check if the proxy class returned a null value.
// Check for and display results and errors
if (response == null)
{
throw new Exception("Could not get response from service");
}
Check if the service returned request-level errors and if so, display them.
if (response.OperationRequest != null
&& response.OperationRequest.Errors != null)
{
printErrors(response.OperationRequest.Errors);
}
Check if the service returned operation-level errors and if so, display them. If not, get a value from the results and display it.
GetAccountBalanceResult[] getAccountBalanceResultArray = response.GetAccountBalanceResult;
if (getAccountBalanceResultArray != null)
{
if (getAccountBalanceResultArray[0].Request.Errors != null)
{
printErrors(getAccountBalanceResultArray[0].Request.Errors);
}
else
{
Console.WriteLine("Account balance: " + getAccountBalanceResultArray[0].AvailableBalance.FormattedPrice.ToString());
}
}
If the HTTP connection was successful, the string returned by
LWP contains the XML response. You can parse
the response with XML::XPath.
To understand the response and display results in Perl
Construct an XML::XPath instance with the response data.
# Check for and display results and errors my $xp = XML::XPath->new(xml => $response->content);
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
sub print_errors {
my ($error_nodeset) = @_;
print "There was an error processing your request:\n";
foreach my $error_node ($error_nodeset->get_nodelist) {
print
" Error code: ", $xp->findvalue("./Code", $error_node), "\n",
" Error message: ", $xp->findvalue("./Message", $error_node), "\n";
}
}
Check if the service returned request-level errors and if so, and display them.
if ( my $error_nodeset = $xp->find("/GetAccountBalanceResponse/OperationRequest/Errors/Error") ) {
print_errors($error_nodeset);
}
Check if the service returned operation-level errors and if so, display them.
if ( my $error_nodeset = $xp->find("/GetAccountBalanceResponse/GetAccountBalanceResult/Request/Errors/Error") ) {
print_errors($error_nodeset);
}
Check if the service returned results, and if so, extract a value and display it.
if ( my $availbalance_node = $xp->find("/GetAccountBalanceResponse/GetAccountBalanceResult/AvailableBalance") ) {
print "Account balance: " . $xp->findvalue("./FormattedPrice", ($availbalance_node->get_nodelist())[0]) . "\n";
}
If the HTTP connection was successful, the data returned by
simplexml_load_file($url) contains the XML data
object for the response. You can examine the data using
accessors on the object.
To understand the response and display results in PHP
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
// Check for and display results and errors
function print_errors($error_nodes) {
print "There was an error processing your request:\n";
foreach ($error_nodes as $error) {
print " Error code: " . $error->Code . "\n";
print " Error message: " . $error->Message . "\n";
}
}
Check if the service returned request-level errors and if so, display them.
if ($xml->OperationRequest->Errors) {
print_errors($xml->OperationRequest->Errors->Error);
}
Check if the service returned operation-level errors and if so, display them.
if ($xml->GetAccountBalanceResult->Request && $xml->GetAccountBalanceResult->Request->Errors) {
print_errors($xml->GetAccountBalanceResult->Request->Errors->Error);
}
Check if the service returned results, and if so, extract a value and display it.
$balance = $xml->GetAccountBalanceResult->AvailableBalance->FormattedPrice;
if ($balance) {
print "Account balance: " . $balance . "\n";
}
For the Python (REST) project, you can process both
request-level and operation-level errors by using the
xml.dom.minidom method
getElementsByTagName(...) to search for
elements named "Errors". You can use a similar technique to
search for and retrieve values from the results.
To understand the response and display results in Python (REST)
Search for elements named "Errors", and display errors if found.
# Check for and display results and errors
errors_nodes = result_xml.getElementsByTagName('Errors')
if errors_nodes:
print 'There was an error processing your request:'
for errors_node in errors_nodes:
for error_node in errors_node.getElementsByTagName('Error'):
print ' Error code: ' + error_node.getElementsByTagName('Code')[0].childNodes[0].data
print ' Error message: ' + error_node.getElementsByTagName('Message')[0].childNodes[0].data
Search for a results value, and display it if found.
availbalance_nodes = result_xml.getElementsByTagName('AvailableBalance')
if availbalance_nodes:
print "Available balance: " + availbalance_nodes[0].getElementsByTagName('FormattedPrice')[0].childNodes[0].data
SOAPpy returns the response XML data as a hierarchy
of attribute values. You can examine the response by accessing
the attributes.
To understand the response and display results in Python (SOAP)
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
# Check for and display results and errors
def print_errors(error_node_or_list):
error_list = error_node_or_list
if isinstance(error_list, SOAPpy.Types.structType):
error_list = [error_list]
print 'There were errors processing your request:'
for error_node in error_list:
print ' Error code: ' + error_node.Code
print ' Error message: ' + error_node.Message
Check if the service returned request-level errors and if so, display them.
if hasattr(result, 'Errors'):
print_errors(result.Errors.Error)
Check if the service returned operation-level errors and if so, display them. If not, check if the service returned results and if so, and display a value.
if hasattr(result, 'GetAccountBalanceResult'):
if hasattr(result.GetAccountBalanceResult, 'Request'):
if hasattr(result.GetAccountBalanceResult.Request, 'Errors'):
print_errors(result.GetAccountBalanceResult.Request.Errors.Error)
if hasattr(result.GetAccountBalanceResult, 'AvailableBalance'):
print 'Available balance: ' + result.GetAccountBalanceResult.AvailableBalance.FormattedPrice
For the Ruby (REST) project, you can use
REXML::Document element path accessors to examine
the response document.
To understand the response and display results in Ruby (REST)
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
# Check for and display results and errors
def print_errors(errors_node)
puts 'There was an error processing your request:'
errors_node.each { |error_node|
puts " Error code: #{error_node.elements['Code'].text}"
puts " Error message: #{error_node.elements['Message'].text}"
}
end
Check if the service returned request-level errors and if so, display them.
if error_nodes = xml.root.elements['OperationRequest/Errors'] print_errors(error_nodes) end
Check if the service returned operation-level errors and if so, display them.
if error_nodes = xml.root.elements['GetAccountBalanceResult/Request/Errors'] print_errors(error_nodes) end
Check if the service returned results, and if so, extract a value and display it.
if availbalance_node = xml.root.elements['GetAccountBalanceResult/AvailableBalance']
puts "Available balance: #{availbalance_node.elements['FormattedPrice'].text}"
end
Ruby's SOAP proxy class returns the XML response data as a hierarchy of attribute values. You can examine the response by accessing the attributes.
To understand the response and display results in Ruby (REST)
Define a support routine to display a set of errors to the screen. Use this routine to display either request-level or operation-level errors.
# Check for and display results and errors
def print_errors(error_nodes)
puts 'There was an error processing your request:'
if error_nodes.class == SOAP::Mapping::Object
error_nodes = [error_nodes]
end
error_nodes.each { |error_node|
puts " Error code: #{error_node.code}"
puts " Error message: #{error_node.message}"
}
end
Check if the service returned request-level errors and if so, display them.
if result.operationRequest['Errors'] print_errors(result.operationRequest.errors.error) end
Check if the service returned operation-level errors and if so, display them. If not, check if the service returned results data and if so, extract and display a value.
if result['GetAccountBalanceResult']
if result.getAccountBalanceResult['Request'] and result.getAccountBalanceResult.request['Errors']
print_errors(result.getAccountBalanceResult.request.errors.error)
end
if result.getAccountBalanceResult['AvailableBalance']
availbalance_node = result.getAccountBalanceResult.availableBalance
puts "Available balance: " + availbalance_node.formattedPrice
end
end