WebRequest JSON Wrapper
Hand-building a JSON request body with string concatenation works right up until a value contains a quote, a backslash, or a newline, at which point it silently produces invalid JSON that the server rejects with a parse error that has nothing obviously to do with the character that caused it. JsonEscape() handles the characters JSON requires escaping - quote, backslash, and the control characters below 0x20 as \u00XX - so a value with untrusted or user-typed content in it still produces valid JSON. JsonBuildObject() builds a flat {"key":"value",...} object from two parallel arrays, escaping every value, for the common case of a simple JSON payload that does not need nested structure. This header does not parse the JSON it receives back. A real parser has to walk arbitrary nesting and nearly always needs its own file - JsonParser is a separate entry in this catalogue for exactly that - and pulling a full parser into a request wrapper would mean every caller who only wants to send a webhook payload drags in code they never use. What belongs here is request correctness: the right Content-Type header, UTF-8 handling on the way out and back, and the WebRequest-not-whitelisted failure that every network call in this catalogue needs handled the same way. WebRequest fails with -1 and GetLastError() equal to 4060, ERR_FUNCTION_NOT_ALLOWED, the first time a URL is used from a terminal that has not had it added to Tools > Options > Expert Advisors > Allow WebRequest for listed URL. WebJsonRequest() detects this specifically, and WebJsonErrorDescription() spells out the fix rather than leaving a bare error number for the log. The header is self-contained - it does not include JsonParser, UrlCodec or TelegramSender from this catalogue. Notes and limitations - This is a request builder and transport wrapper, not a parser. outBody is the raw response text; read a field out of it with JsonParser or your own string search, this header does not interpret it. - WebRequest only reaches the network when its URL's domain is on the terminal's allow-list, and that list is per terminal installation, not per script - every machine this is downloaded to needs the same one-time setup, on both MQL4 and MQL5. - Request and response bodies are sent and read as UTF-8. A server that replies in a different encoding will come back as mis-decoded text. - No retry, no rate limiting, no authentication beyond whatever you put in extraHeaders (such as an Authorization header) - this is a thin wrapper, not an HTTP client framework.
Script · MT4 · v1.0 · Free
About WebRequest JSON Wrapper
WebRequest JSON Wrapper is a script for MetaTrader 4. It has 1 setting, such as Test url. MQL source code included.
Files in the download: WebJson.mqh, WebJson.txt, WebJsonDemo.mq4.
Settings
| Setting | Default | What it does |
|---|---|---|
| Test url | https://httpbin.org/get | Public echo endpoint used for the live GET example |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT4 Script · MQL4 Source Code