Using the Response

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

  1. 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());
            }
        }
    	
  2. 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" );
    	
  3. 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() );
                }
    	
  4. 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.

If the HTTP connection was successful, the string returned by LWP contains the XML response. You can parse the response with XML::XPath.

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.

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.

SOAPpy returns the response XML data as a hierarchy of attribute values. You can examine the response by accessing the attributes.

For the Ruby (REST) project, you can use REXML::Document element path accessors to examine the response document.

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.