matlab.net.http.ResponseMessage.complete - Process or reprocess response payload Content-Type - MATLAB (original) (raw)

Main Content

Class: matlab.net.http.ResponseMessage
Namespace: matlab.net.http

Process or reprocess response payload Content-Type

Syntax

Description

[msg](#ref%5Fq3fxxekie7-msg) = complete([msg](#ref%5Fq3fxxekie7-msg)) converts the msg.Body.Payload property tomsg.Body.Data using the current value of the Content-Type header field in msg.

[msg](#ref%5Fq3fxxekie7-msg) = complete([msg](#ref%5Fq3fxxekie7-msg),`consumer`) returns a copy of the message with msg.Body.Payload processed by a matlab.net.http.io.ContentConsumer. The consumer might store its result inmsg.Body.Data or process it in some other manner.

Use the complete method when:

If there was an exception processing the received message, or if you set theHTTPOptions.SavePayload property when you sent the request, the Body.Payload in this response message contains the original payload (if any). In this case, modify the header of this message to add or correct the Content-Type field. Then call the complete method to process the response as if the server had inserted that Content-Type field originally. The result is new contents in Body.Data and/orData processed by the specifiedconsumer.

If Body.Payload is set, then this method ignores the current value ofBody.Data and reprocesses that payload based on Content-Type. This case occurs for a conversion error or if you specifiedSavePayload. But if conversion of the incoming data succeeded originally, but was incorrect, Body.Data is set andBody.Payload might be empty. In this case, change the ContentTypeField in the received message to the desired type and then call this method. complete attempts to convert the data back to a payload based on the Body.ContentType property used to convert it originally. Then it is reconverted using the new Content-Type header in the response message. If Data is not empty, then the returnedBody.Payload is set.

If you specified SavePayload when sending the message, complete uses the original payload that was preserved in Body.Payload instead, with no loss of information.

If the ResponseMessage.Completed property is set,complete does nothing. In a message that contains aBody, this property is normally set only ifmsg.Body.Payload has been set.

example

Input Arguments

Output Arguments

Examples

expand all

Assume that the server returned a response containing a JSON string but specified a Content-Type field of text/plain instead of application/json. In this case, Body.Payload is empty and msg.Body.Data contains an ASCII string (since the default charset for text/plain is us-ascii). To process this data and obtain a JSON structure:

response = response.changeFields('Content-Type','application/json'); response = response.complete(); data = response.Body.Data;

The call to complete converts Body.Data to Body.Payload using us-ascii encoding. The method then reconverts Body.Payload to utf-8 before processing it as a JSON string and storing the result in Body.Data. This conversion does not retain any non-ASCII characters that were garbled when converting the original payload using text/plain but preserves the original ASCII data.

Version History

Introduced in R2016b