Help with WebRequest POST generating invalid JSON body

To add comments, please log in or register
mrwick
42
mrwick  

Hi

I'm trying to post the below to my express js endpoint 

I have a function below but it's posting an invalid JSON body to my express js.


void PostOrderStatusAPI()
  {
   if(MQLInfoInteger(MQL_TESTER)) { }
   else
     {
// JSON text to send
   string strJsonText = "{\"key\": \"value\"}";


   // Text must be converted to a uchar array. Note that StringToCharArray() adds 
   // a nul character to the end of the array unless the size/length parameter
   // is explicitly specified 
   uchar jsonData[];
   StringToCharArray(strJsonText, jsonData, 0, StringLen(strJsonText));

   // Use MT4's WebRequest() to send the JSON data to the server.
   char serverResult[];
   string serverHeaders;
   int res = WebRequest("POST", "http://x.x.x.x/api/updatetradingstatus/", "", "", 10000, jsonData, ArraySize(jsonData), serverResult, serverHeaders);

   Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));


     }


  }


Body

{ '{"key": "value"}': '' }

How do I correct this? 


thanks

nicholish en
2694
nicholish en  
mrwick:

Hi

I'm trying to post the below to my express js endpoint 

I have a function below but it's posting an invalid JSON body to my express js.



Body

How do I correct this? 


thanks

You should be using a JSON serializer. 

#include <jason.mqh>

void OnStart() {
   CJAVal json;
   json["key"] = "value";
   Print(json.Serialize());
}

https://www.mql5.com/en/code/13663

JSON Serialization and Deserialization (native MQL)
JSON Serialization and Deserialization (native MQL)
  • www.mql5.com
ForecastOscilator_HTF The ForecastOscilator indicator with the timeframe selection option available in the input parameters. Flat_HTF The Flat indicator with the timeframe selection option available in the input parameters. FX5_SelfAdjustingRSI_HTF The...
mrwick
42
mrwick  
nicholish en:

You should be using a JSON serializer. 

https://www.mql5.com/en/code/13663

You mean something like this? It's still show invalid body. Could you give me an example how I can do a POST on this?

void PostOrderStatusAPI(string pair, int order_type)
  {
   if(MQLInfoInteger(MQL_TESTER)) { }
   else
     {
// JSON text to send
 //  string strJsonText= "{\"key\"= \"value\"};
    CJAVal json;

   json["key"] = "value";


   // Text must be converted to a uchar array. Note that StringToCharArray() adds 
   // a nul character to the end of the array unless the size/length parameter
   // is explicitly specified 
   uchar jsonData[];
   StringToCharArray(json.Serialize(), jsonData, 0, StringLen(json.Serialize()),CP_UTF8);

   // Use MT4's WebRequest() to send the JSON data to the server.
   char serverResult[];
   string serverHeaders;
   int res = WebRequest("POST", "http://x.x.x.x/api/updatetradingstatus/", "", "", 10000, jsonData, ArraySize(jsonData), serverResult, serverHeaders);

   Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));


     }


  }
To add comments, please log in or register