Trying to connect MT4 to Notion.

 

So, I want to create a EA to communicate with my Notion DB each time I close a trade. My main problem is that I cannot connect to my Notion DB; server throws me a 5203 error. Don't know if I am doing the headers wrong or what else I am doing wrong.

Here is my code:

#include <JAson.mqh>;

extern string YourDatabaseID = "b11b2a2bd1d84184b238530fb8879186";
string token = "secret_4eovTkH9D8XYxCaDdkD7mtEi3vSTBUi7dvBDYZCWr6d";
string notionUrl = "https://api.notion.com/v1/databases/" + YourDatabaseID;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //string headers = "{\"Authorization\": \"Bearer secret_4eovTkH9D8XYxCaDdkD7mtEi3vSTBUi7dvBDYZCWr6d\""
                     //+ ", \"Content-Type\": \"application/json\""
                     //+ ", \"Notion-Version\": \"2022-02-22\"}";
   string headers;
   CJAVal h;
   h["Authorization"] = "Bearer " + token;
   h["Content-Type"] = "application/json";
   h["Notion-Version"] = "2022-02-22";
   headers = h.Serialize();
   Print(headers);
   string r = SendResquest("POST", notionUrl, "", headers, 5000);
//---
   
//---
   return(INIT_SUCCEEDED);
}
string SendResquest(string httpType, string url, string bodyData = "", string headers = "", int timeout=5000)
{
   uchar bodyDataCharArray[];
   uchar resultDataCharArray[];
   string resultHeader;
   ArrayResize(bodyDataCharArray, StringToCharArray(bodyData, bodyDataCharArray)-1);

   int response = WebRequest(httpType, url, headers, timeout, bodyDataCharArray, resultDataCharArray, resultHeader);

   if(response == 200) {
      Print("Yes");
   } else {
      Print("Error when trying to call API : ", GetLastError());
      return "";
   }
    
   return "";
}
 
Genaro Cancino: So, I want to create a EA to communicate with my Notion DB each time I close a trade. My main problem is that I cannot connect to my Notion DB; server throws me a 5203 error. Don't know if I am doing the headers wrong or what else I am doing wrong. Here is my code:

Don't do such things in the OnInit() event handler. You are blocking the thread and preventing a proper initialisation process. Do it in one of the other event handlers, or use a Script instead of an EA.

 
Fernando Carreiro #:

Don't do such things in the OnInit() event handler. You are blocking the thread and preventing a proper initialisation process. Do it in one of the other event handlers, or use a Script instead of an EA.

Hello, Fernando. Thanks for your response.

I tried in a script -as you said- and got the same error: "Error when trying to call API: 5203".
Put the code OnStart() function of the script.
 
Genaro Cancino #: I tried in a script -as you said- and got the same error: "Error when trying to call API: 5203". Put the code OnStart() function of the script.

Before trying to do what you want to do, first do some testing with very simple web access with a "GET", without headers and such, to make sure you are doing things correctly. Then slowly work your way up while debugging it.

Also, make sure that the URL has been granted permission in the MetaTrader settings.

 
Fernando Carreiro #:

Before trying to do what you want to do, first do some testing with very simple web access with a "GET", without headers and such, to make sure you are doing things correctly. Then slowly work your way up while debugging it.

Also, make sure that the URL has been granted permission in the MetaTrader settings.

I did a test with google and it worked nice and put the code in OnInit() function just to test, nothing more. My code needs to go in OnTrade(). I was just testing how I would post the data I need to.

int OnInit()
{  
   char d[];
   char d2[];
   string hRes;
   int r = WebRequest("GET", "https://google.com", "", 5000, d, d2, hRes);
   if ( r != 200 ) {
      Print("No" + (string)GetLastError());
   } else {
      Print("Yes");
      string res = hRes;
      Print(res);
   }
   
   return(INIT_SUCCEEDED);
}
Guess now, I can do some testing with the Notion API GET functions and see what it's happening.
 
Did you ever get anywhere with this? Im looking at a solution also but having troubles
 
Terry McCall #: Did you ever get anywhere with this? Im looking at a solution also but having troubles

If you are referring to your question ... MT4 Script File path issues, then this thread is unrelated. You have been given an answer on your thread, however.

 

I trying to do as well I fail Any news about this project?

Reason: