Send files via Socket

 

Hi,


   A question, I can send a file via webrequest using multipart/form-data, because in webrequest function there is the *"Header field" and the body field, it works perfectly, but how to send files using sockets? On socket how do I send multipart?

POST /MyURL HTTP/1.1
Cache-Control: no-cache
Postman-Token: 4f9c9910-aaa1-198b-97ce-373a612ea3d0
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="chat_id"

839815629
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="photo"

https://conteudo.imguol.com.br/c/noticias/de/2019/11/25/mcdonalds-1574721282710_v2_1254x836.png
------WebKitFormBoundary7MA4YWxkTrZu0gW--

The follow code works (with webrequest)

 string url=StringFormat("%s/bot%s/sendPhoto",TELEGRAM_BASE_URL,m_token);
      uchar data[];
      //--- 
      ArrayAdd(data,"POST /bot"+token+"/sendPhoto HTTP/1.1\r\n");
      ArrayAdd(data,"Host: api.telegram.org\r\n");
      ArrayAdd(data,"Content-Type: multipart/form-data; boundary="+hash+"\r\n");
      ArrayAdd(data,"Connection: keep-alive\r\n");
      ArrayAdd(data,"\r\n");


      ArrayAdd(data,"--"+hash+"\r\n");
      ArrayAdd(data,"Content-Disposition: form-data; name=\"chat_id\"\r\n");
      ArrayAdd(data,"\r\n");
      ArrayAdd(data,IntegerToString(_chat_id));
      ArrayAdd(data,"\r\n");

      ArrayAdd(data,"--"+hash+"\r\n");
      ArrayAdd(data,"Content-Disposition: form-data; name=\"photo\"; 
      ArrayAdd(data,"\r\n");
      ArrayAdd(data,"https://conteudo.imguol.com.br/c/noticias/de/2019/11/25/mcdonalds-1574721282710_v2_1254x836.png");
      ArrayAdd(data,"\r\n");

      ArrayAdd(data,"--"+hash+"--");
      string headers="Content-Type: multipart/form-data; boundary="+hash+"\r\n";
      int res=WebRequest("POST",url,headers,_timeout,data,result,result_headers);


I tried the follow but headers are not avaliable

{
         uchar req[];
         SocketTlsSend(socket_tlg,data,ArraySize(data));
         int len=(int)SocketIsReadable(socket_tlg);
         int rsp_len=SocketTlsReadAvailable(socket_tlg,req,len);
         string  rlt=CharArrayToString(req,0,rsp_len);
         Print(rlt);
         double a=0;
}


Thanks in advance

Documentation on MQL5: Network Functions / WebRequest
Documentation on MQL5: Network Functions / WebRequest
  • www.mql5.com
To use the WebRequest() function, add the addresses of the required servers in the list of allowed URLs in the "Expert Advisors" tab of the "Options" window. Server port is automatically selected on the basis of the specified protocol - 80 for "http://" and 443 for "https://". The WebRequest() function is synchronous, which means its breaks the...