Search code examples
delphiindyindy10delphi-xe8

striping out the "content-type" from TIdMultiPartFormDataStream


This is kind of related to this post. I am trying to post some form data using TIdHTTP and TIdMultiPartFormDataStream, but when monitoring the communication using Wireshark, each form field gets a content-Type: text/plain attached to it and for some reason the server that I am sending these stuff to does not like it. Is there a way that I can make sure only the name and value is sent? The Content-Transfer was also being added and I was able to remove that using:

aFieldItem := PostStream.AddFormField(fName, fValue);
aFieldItem.ContentTransfer := '';

but I cannot find any way to get rid of content type. At this moment the data that is being sent looke like this (in Wireshark)

Boundary: \r\n----------051715151353026\r\n
Encapsulated multipart part:  (text/plain)
    Content-Disposition: form-data; name="description"\r\n
    Content-Type: text/plain\r\n
    Line-based text data: text/plain
        \r\n
        Testing new AW Mobile

and I want it to look like:

Boundary: \r\n------WebKitFormBoundary32hCBG8zkGMBpxqL\r\n
Encapsulated multipart part:
    Content-Disposition: form-data; name="description"\r\n
    Data (21 bytes)
        Data: 0d0a5465737420616e6420747261636520636f6d6d
        Length: 21

Thank you Sam


Solution

  • HTML5 Section 4.10.22.7 alters how RFC 2388 applies to webform submissions:

    The parts of the generated multipart/form-data resource that correspond to non-file fields must not have a Content-Type header specified. Their names and values must be encoded using the character encoding selected above (field names in particular do not get converted to a 7-bit safe encoding as suggested in RFC 2388).

    This is different from RFC 2388:

    As with all multipart MIME types, each part has an optional "Content-Type", which defaults to text/plain.

    Your server is clearly expecting the HTML5 behavior.

    The Content-Type header on each MIME part added to TIdMultipartFormDataStream is hard-coded and cannot be removed without altering TIdMultipartFormDataStream's source code can be omitted by setting the TIdFormDataField.ContentType property to a space character (not a blank string, like the ContentTransfer property allows):

    aFieldItem := PostStream.AddFormField(fName, fValue);
    aFieldItem.ContentTransfer := '';
    aFieldItem.ContentType := ' '; // <-- here
    

    If you set the ContentType property to a blank string, it will set the Content-Type header to application/octet-stream, but assigning a space character instead has a side effect of omitting the header when the property setter parses the new value.

    That being said, I have already made some changes to TIdMultipartFormDataStream to account for this change in webform submission in HTML5, but I have not finalized and released it yet.