protected function sendApiMessage($uri, array $message, Result $result)
    {
        // Convert message to XML. XML requires 1 top level tag, so add one.
        // The tagname is ignored by the Acumulus WebAPI.
        $message = $this->convertArrayToXml(array('myxml' => $message));
        // Keep track of communication for debugging/logging at higher levels.
        $result->setRawRequest($message);

        $rawResponse = $this->sendHttpPost($uri, array('xmlstring' => $message), $result);
        $result->setRawResponse($rawResponse);

        if (empty($rawResponse)) {
            // CURL may get a time-out and return an empty response without
            // further error messages: Add an error to tell the user to check if
            // the invoice was sent or not.
            $result->addError(701, 'Empty response', '');
        } elseif ($this->isHtmlResponse($result->getRawResponse())) {
            // When the API is gone we might receive an html error message page.
            $this->raiseHtmlReceivedError($result->getRawResponse());
        } else {
            // Decode the response as either json or xml.
            $response = array();
            $pluginSettings = $this->config->getPluginSettings();

            if ($pluginSettings['outputFormat'] === 'json') {
                $response = json_decode($result->getRawResponse(), true);
            }
            // Even if we pass <format>json</format> we might receive an XML
            // response in case the XML was rejected before or during parsing.
            // So if the response is null we also try to decode the response as
            // XML.
            if ($pluginSettings['outputFormat'] === 'xml' || $response === null) {
                try {
                    $response = $this->convertXmlToArray($result->getRawResponse());
                } catch (RuntimeException $e) {
                    // Not an XML response. Treat it as an json error if we were
                    // expecting a json response.
                    if ($pluginSettings['outputFormat'] === 'json') {
                        $this->raiseJsonError();
                    }
                    // Otherwise treat it as the XML exception that was raised.
                    throw $e;
                }
            }
            $result->setResponse($response);
        }

        return $result;
    }