Main Site Documentation

Gadgeteer.Networking.Responder auto-set mimetypes?


#1

I’m looking at Gadgeteer.Net.Responder and some of the methods say it auto-sets the “correct” mimetype, but I don’t like the mimetypes. One example is the string option that says it returns as text/plain, but I want text/xml or possibly even HTML. Is there a way to return a string and set my own mimetype? Converting to a byte array causes excess and unnecessary memory usage.

Or, have I just totally missed some class that is perfect?


#2

In case you are still looking for this information.

Take a look at the source code of the Responder. It does convert the provided text to byte array internally, so there is no way around it.


/// <summary>
        /// Updates the data with which the web event responds and sets the correct MIME type.  
        /// </summary>
        /// <param name="text">The plain/text to be published.</param>
        public void Respond(string text)
        {
            if (CheckResponded()) return;

            if (text != null)
            {
                webEvent.ContentType = "text/plain";
                webEvent.ResponseData = Encoding.UTF8.GetBytes(text);
            }
            SendResponse();
        }

So for non-included mime types you will need to use:


/// <summary>
        ///  Updates the data with which the WebEvent responds. The MIME type can be set manually.
        /// </summary>
        /// <param name="data">The data to be streamed. </param>
        /// <param name="ContentType">The MIME type of the outgoing data.</param>
        public void Respond(byte[] data, string ContentType)
        {
            if (CheckResponded()) return;

            if (data != null)
            {
                webEvent.ContentType = ContentType;
                webEvent.ResponseData = data;
            }
            SendResponse();
        }