Skip to main content

Including a Request Body

Including a Request Body

An HTTP request can include either a request body or form data. To include a request body, do the following:

  1. Create an instance of %GlobalBinaryStreamOpens in a new tab or a subclass. Use this instance for the EntityBody property of your HTTP request.

    Note that %GlobalBinaryStreamOpens in a new tab is deprecated but is still supported for use in this way. It is not recommended to substitute a different stream class for this use case.

  2. Use the standard stream interface to write data into this stream. For example:

     Do oref.EntityBody.Write("Data into stream")

For example, you could read a file and use that as the entity body of your custom HTTP request:

 set file=##class(%File).%New("G:\customer\catalog.xml")
 set status=file.Open("RS")
 if $$$ISERR(status) do $System.Status.DisplayError(status)
 set hr=##class(%Net.HttpRequest).%New()
 do hr.EntityBody.CopyFrom(file)
 do file.Close()

Sending a Chunked Request

If you use HTTP 1.1, you can send an HTTP request in chunks. This involves setting the Transfer-Encoding to indicate that the message is chunked, and using a zero-sized chunk to indicate completion.

Chunked encoding is useful when the server is returning a large amount of data and the total size of the response is not known until the request is fully processed. In such a case, you would normally need to buffer the entire message until the content length could be computed (which %Net.HttpRequestOpens in a new tab does automatically).

To send a chunked request, do the following:

  1. Create a subclass of %Net.ChunkedWriterOpens in a new tab, which is an abstract stream class that defines an interface for writing data in chunks.

    In this subclass, implement the OutputStream() method.

  2. In your instance of %Net.HttpRequestOpens in a new tab, create an instance of your %Net.ChunkedWriterOpens in a new tab subclass and populate it with the request data that you want to send.

  3. Set the EntityBody property of your %Net.HttpRequestOpens in a new tab instance equal to this instance of %Net.ChunkedWriterOpens in a new tab.

    When you send the HTTP request (see Sending the HTTP Request), it calls the OutputStream() method of the EntityBody property.

In your subclass of %Net.ChunkedWriterOpens in a new tab, the OutputStream() method should examine the stream data, decide whether to chunk it and how to do so, and invoke the inherited methods of the class to write the output.

The following methods are available:

WriteSingleChunk()

Accepts a string argument and writes the string as non-chunked output.

WriteFirstChunk()

Accepts a string argument. Writes the appropriate Transfer-Encoding heading to indicate a chunked message, and then writes the string as the first chunk.

WriteChunk()

Accepts a string argument and writes the string as a chunk.

WriteLastChunk()

Accepts a string argument and writes the string as a chunk, followed by a zero length chunk to mark the end.

If non-null, the TranslateTable property specifies the translation table to use to translate each string as it is written. All of the preceding methods check this property.

FeedbackOpens in a new tab