title: How to send a POST with a JSON in a WebRequest() call using MQL4?
body: <p>I would like to send a <strong><code>POST</code></strong> from <code>MQL4</code>-script, using a JSON-format to a Node-server.</p>
  
  <p>I've tried the <strong><code>webRequest()</code></strong> standard function in <code>MQL4</code>, based on the following documentation, but it did NOT success.</p>
  
  <blockquote>
    <p>From MQL4 Documentation:</p>
  </blockquote>
  
  <pre><code>Sending simple requests of type "key=value" using the header `Content-Type: application/x-www-form-urlencoded`.    
  
      int  WebRequest( const string  method,           // HTTP method  
                       const string  url,              // URL 
                       const string  cookie,           // cookie 
                       const string  referer,          // referer 
                       int           timeout,          // timeout 
                       const char   &amp;data[],           // the array of the HTTP message body 
                       int           data_size,        // data[] array size in bytes 
                       char         &amp;result[],         // an array containing server response data 
                        string      &amp;result_headers    // headers of server response
                       );
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Sending a request of any type specifying the custom set of headers for a more flexible interaction with various Web services.
  
      int  WebRequest( const string  method,           // HTTP method 
                       const string  url,              // URL 
                       const string  headers,          // headers  
                       int           timeout,          // timeout 
                       const char   &amp;data[],           // the array of the HTTP message body 
                       char         &amp;result[],         // an array containing server response data 
                       string       &amp;result_headers    // headers of server response 
                       );
  
  
  Parameters
  
      method          [in]  HTTP method.
      url             [in]  URL.
      headers         [in]  Request headers of type "key: value", separated by a line break "\r\n".
      cookie          [in]  Cookie value.
      referer         [in]  Value of the Referer header of the HTTP request.
      timeout         [in]  Timeout in milliseconds.
      data[]          [in]  Data array of the HTTP message body.
      data_size       [in]  Size of the data[] array.
      result[]       [out]  An array containing server response data.
      result_headers [out]  Server response headers. 
  
  Returned value:
      HTTP server response code or -1 for an error.
  </code></pre>
  
  <p>Does anyone know how to perfom it?</p>
  
  <hr>
  
  <h2>UPDATE:</h2>
  
  <hr>
  
  <p>Here is the code on the <code>MQL4</code>-script side :</p>
  
  <pre><code>#include &lt;Json\hash.mqh&gt;
  #include &lt;Json\KoulJSONMgmt.mqh&gt;
  void OnStart()
  
      {
         string strParam = StringConcatenate("{","\"currency\"",":","\"",Symbol(),"\"",",","\"timeframe\"",":","\"",IntegerToString(Period()),"\"",",","\"ticktime\"",":","\"",TimeToString(TimeLocal(),TIME_DATE|TIME_SECONDS),"\"",",","\"bid\"",":",DoubleToString(MarketInfo(Symbol(),MODE_BID),4),",","\"ask\"",":",DoubleToString(MarketInfo(Symbol(),MODE_ASK),4),",","\"spread\"",":",DoubleToString(MarketInfo(Symbol(),MODE_SPREAD),0),"}");
  
  
          JSONParser *parser = new JSONParser();
          JSONValue *jv = parser.parse(strParam);
          string strJson = jv.toString();
          if (jv == NULL) {
              Print("error:"+(string)parser.getErrorCode()+parser.getErrorMessage());
          } else {
              Print("PARSED:"+strJson);
      //Example of json String :
      //EURUSD,M15: PARSED:{"bid" : 1.1152,"ask" : 1.1154,"spread" : 13,"ticktime" : "2016.10.10 16:24:01","currency" : "EURUSD","timeframe" : "15"}
          }
  
            string cookie=NULL,headers;
            char post[],result[];
            int res;
            string strResult,result_header;
  
            headers = "application/json";
            prmUrl=StringConcatenate("http://localhost/api"+"/"+"ticks");
            //--- Reset the last error code
            ResetLastError();
            int timeout=1000; //--- Timeout below 1000 (1 sec.) is not enough for slow Internet connection
  
            int intHostNameLength=StringLen(prmParameter);
            StringToCharArray(prmParameter,post,0,intHostNameLength);
  
            res=WebRequest("POST",prmUrl,headers,timeout,post,result,result_header);
  
            //--- Checking errors
            if(res==-1)
              {
               Print("Error in WebRequest. Error code  =",GetLastError());
               //--- Perhaps the URL is not listed, display a message about the necessity to add the address
               Print("Add the address '"+prmUrl+"' in the list of allowed URLs on tab 'Expert Advisors'","Error",MB_ICONINFORMATION);
              }
            else
             {
                 for(int i=0;i&lt;ArraySize(result);i++)
                 {
  
                     if( (result[i] == 10) || (result[i] == 13)) {
                        continue;
                     } else {
                        strResult += CharToStr(result[i]);
                     }
                 }
                 ArrayCopy(strResult,result,0,0,WHOLE_ARRAY);
  
             }
             Print(strResult);
  
      }
  </code></pre>
  
  <p>And the Node side is :</p>
  
  <p>server.js</p>
  
  <pre><code>//Create new Tick
  app.post('/api/ticks', function(req, res) {
      console.log('Inserting New Tick');
  
      var tick = req.body;
      console.log('&gt;'+JSON.stringify(tick,null,4));
  
      Tick.addTick(tick, function(err, tick){
          if(err) res.json(err);
          res.json(tick);
      });
  });
  </code></pre>
  
  <p>and in model ticks.js</p>
  
  <pre><code>var mongoose = require('mongoose');
  
  
  // User Schema
  var TickSchema = mongoose.Schema({
      currency:{
          type: String
      },
      timeframe: {
          type: String
      },
      ticktime: {
          type: Date
      },
      bid: {
          type: Number
      },
      ask: {
          type: Number
      },
      spread: {
          type: Number
      },
      createddate :{
          type: Date,
          default:  Date.now
      }
  }, {collection : 'fxTicks'});
  
  var Tick = module.exports = mongoose.model('Tick', TickSchema);
  
  //Create New Tick
  module.exports.addTick = function(tick, callback){
      Tick.create(tick, callback);
  };
  
  // Get Ticks
  module.exports.getTicks = function(callback, limit){
      Tick.find(callback).limit(limit);
  };
  </code></pre>