How to send "put" request in MQL?

 

Hi, I am new to mql i am trying to update data in my firebase database. I successfully sent "POST" request but it appends to previous data and i want to update the previous data so i need to do "PUT" request. Here is my code.


void postJson()

{

string strJsonText = "{\"key\": \"value\"}";

   uchar jsonData[];

   StringToCharArray(strJsonText, jsonData, 0, StringLen(strJsonText));

  char serverResult[];

   string serverHeaders = "content-type: application/json;\n";

   int res = WebRequest("PUT", "https://XXXXXXXX.firebaseio.com/orders.json", "", "", 10000, jsonData, ArraySize(jsonData), serverResult, serverHeaders);

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

}
 

Hi  Momin Khan

Probably got your answer, but for anyone out thier looking to do a "PUT" to firebase.

I have an array of pairs as a .mqh file for include.

#include <pairs.mqh>
#include <Arrays\ArrayObj.mqh>
#include <JAson.mqh>
//--- parameters for receiving data from the terminal
string url = "https://xxxxx.firebaseio.com/prices.json";
input ENUM_TIMEFRAMES timeFrame = PERIOD_D1;

I was getting a 400 response for a while but checked with postman and it was just the body json malformed when Serializing.

void postJson(ENUM_TIMEFRAMES tf)

   {
    int size = ArraySize(pairsList);
    CJAVal data;
    for(int i = size - 1; i >= 0; i--)
       {
        string p = pairsList[i];
        data[i]["pair"] = p;
        data[i]["open"] = iOpen(p, tf, 0);
        data[i]["close"] = iClose(p, tf, 0);
        data[i]["value"] = (getPercentageChange(p, tf));
        data[i]["updated"] = TimeToString(TimeCurrent());
       }
    string prices = data.Serialize();
    char serverResult[];
    uchar da[];
    StringToCharArray(prices, da, 0, StringLen(prices));
    string serverHeaders = "content-type: application/json;\n";
    int res = WebRequest("PUT", url, NULL, NULL, 1000, da, ArraySize(da), serverResult, serverHeaders);
    Print("Web request result: ", res, ", error: #", (res == -1 ? GetLastError() : 0));
   }

We post the Auth section when done


 
Momin. Thanks for this example!
 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".
Reason: