0

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

Community
  • 1
  • 1
Sam
  • 2,473
  • 3
  • 18
  • 29

1 Answers1

4

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.

Community
  • 1
  • 1
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks. Any suggestion as to how I can handle this in the meantime? – Sam May 18 '15 at 03:20
  • As I said, you would have to alter TIdMultipartFormDataStream's source code, and then recompile Indy. Alternatively, copy its source code to a new unit/class in your project that you can then alter as needed. Or just write your own MIME data into a standard TStream, such as TMemoryStream. I have no ETA on when the new TIdMultipartFormDataStream will be ready for release. – Remy Lebeau May 18 '15 at 14:51
  • The 3rd option (MIME data into a standard TStream, such as TMemoryStream) sounds best. Do you have an example of doing that? I need to post a bunch of name-value pairs and a file and the end. Thanks – Sam May 18 '15 at 16:51
  • Actually I found an example [Here](http://forums2.atozed.com/viewtopic.php?f=7&t=14992) that made it work – Sam May 18 '15 at 21:01
  • Here is the discussion that talks about the changes needed to make `TIdMultipartFormDataStream` work with HTML5 servers: [Removing lines from TIdMultiPartFormDataStream](http://forums2.atozed.com/viewtopic.php?f=7&t=26683) – Remy Lebeau May 18 '15 at 21:06
  • Thanks, this saved my day, I was going crazy because I could not send any plain form fields together with a file post. – MichaSchumann Aug 16 '16 at 13:57