Loading…

Multipart Form Data Builder

WebRequest can POST a body on both platforms, but neither MQL4 nor MQL5 has a form widget or a library that assembles a multipart/form-data body the way a browser does when a file input is submitted. That body shape is a fixed structure - a boundary marker, a header block, and a payload, repeated once per field or file, with a closing boundary at the end - and it is exactly what most ordinary HTTP upload endpoints still expect, screenshots, CSV reports and log files included. The one design decision that actually matters here: text fields and file bytes are appended through different paths. A text field's value goes through UTF-8 conversion, because that is what a text field is. File bytes are copied into the body raw, with no conversion at all - a PNG screenshot or a zipped log is binary, not valid UTF-8, and running it through a string-based append would silently corrupt it. A single 0x00 byte is enough to truncate a naive C-string-style append outright, and both platforms' strings are UTF-16 under the hood, so treating raw file bytes as string data risks exactly that kind of silent corruption. AddFile() takes a uchar array specifically so there is no string anywhere in the file's path through the builder to accidentally reinterpret it through. The boundary marker is generated once per instance from MathRand. It does not need to be cryptographically random - its only job is to not collide with a byte sequence that happens to occur inside the payload, and sixteen random hex digits appended to a fixed prefix makes that astronomically unlikely for anything a trading terminal would ever upload. Field and file names are escaped per RFC 7578 before being placed inside the Content-Disposition header's quoted string, so a name containing a quote or a backslash cannot break out of the quotes it sits inside and corrupt the part boundary that follows it. The only real port change between platforms is a typing one: MQL5's StringToCharArray() requires a uchar destination array rather than the char array MQL4 accepts for the same call. The class and demo declare uchar arrays directly on MQL5, which also removes a per-byte cast the MQL4 source needed. The raw-byte AddFile() path, the RFC 7578 escaping and the boundary generation are otherwise unchanged, because none of it touches a trading, chart or account API at all. Notes and limitations - This assembles the request BODY only. Sending it still needs WebRequest with the URL whitelisted in Tools > Options > Expert Advisors (MQL4) or Tools > Options > Expert Advisors (MQL5, same dialog), and the exact WebRequest parameter shape varies slightly by terminal build - check your build's signature rather than copying the demo's Print() line verbatim into a live EA. - No chunked or streaming upload - the whole body is assembled in memory before Build() returns it. Fine for a screenshot or a log file; not designed for uploading anything the terminal cannot comfortably hold in RAM twice over (once in the builder, once in WebRequest's own buffer). - The boundary is generated from MathRand(), not a cryptographic source. That is adequate for its actual job - avoiding an accidental collision with the payload - and would not be adequate for anything security sensitive, which a form boundary has never needed to be. - Field and file NAME values are escaped for the quoted-string context they sit in; this class does not otherwise validate or sanitise a file's CONTENTS, which is the caller's responsibility same as any other upload.

Script · MT4 · v1.0 · Free

About Multipart Form Data Builder

Multipart Form Data Builder is a script for MetaTrader 4. MQL source code included.

Files in the download: MultipartFormBuilder.mqh, MultipartFormBuilder.txt, MultipartFormBuilderDemo.mq4.

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria