Main Content

matlab.net.http.HTTPException class

Package: matlab.net.http
Superclasses: MException

Exception thrown by HTTP services

Description

The HTTPException class contains information about errors.

The RequestMessage.send method throws an HTTP exception when an error occurs after it attempts to send a message. For instance, the method throws an exception when:

  • Something fails during transmission and the server does not receive the message. Possible causes of failure include a network problem, timeout, or bad URI.

  • The server receives the message, but something fails while it is sending a response.

  • The server receives the message and sends a response, but the response cannot be converted based on its Content-Type. For example, a bad JSON string is received.

If an error occurs before the RequestMessage.send method attempts to send the message, then it throws a standard MException instead.

The HTTPException describes the error in the MException.cause property and returns a history of the transaction. The History property contains the message that was sent and the message that was received, if any.

Properties

expand all

Request message as completed, specified as a matlab.net.http.RequestMessage object. Request contains the last message that was sent or would have been sent. If this message header was successfully sent, then it is the same as the last entry in the History.Request property. Otherwise, the last entry in the History property does not contain the message you attempted to send.

Attributes:

GetAccess
public
SetAccess
private

URI for the last message that was sent or would have been sent, specified as a matlab.net.URI object.

Attributes:

GetAccess
public
SetAccess
private

History of the transaction, specified as a vector of matlab.net.http.LogRecord objects. History only contains messages whose headers were successfully sent or received. If an exception occurs when sending or receiving a message header, then History does not contain that message. If an error occurs while sending or receiving the payload, or during conversion of the data to or from the payload, then the MessageBody Payload property and/or Data properties might not be set.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Send a message to the website in the url variable. This destination returns a message whose Payload property contains an invalid JPEG image for Content-Type image/jpeg.

try
    resp = RequestMessage().send(url);
catch e
    if isa(e,'matlab.net.http.HTTPException')
        response = e.History(end).Response;      
        if ~isempty(response)
            data = response.Body.Data;
            payload = response.Body.Payload
        end
    end
end

The payload variable contains the bytes that were received as a uint8 vector. Since the Content-Type is not character-based, the data variable is empty.

Version History

Introduced in R2016b