Help with WebRequest POST generating invalid JSON body

mrwick
47
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
2758
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
47
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));


     }


  }
Yashar Safarzadeh
297
Yashar Safarzadeh  

Dear nicholish en


would you please provide the code to de serialize this JSON URL?

i tried a lot but all with false results.

https://search.codal.ir/api/search/v2/q?&Audited=true&AuditorRef=-1&Category=-1&Childs=true&CompanyState=-1&CompanyType=-1&Consolidatable=true&IsNotAudited=false&Length=-1&LetterType=-1&Mains=true&NotAudited=true&NotConsolidatable=true&PageNumber=1&Publisher=false&TracingNo=-1&search=true