Webrequest() strange behaviour

 

Hey. I am having some problems with WebRequest. I used this function numerous times in the past - never had any problem. 

The problem I now face is related to accessing binance API. When you open those 2 urls simply in a browser:

https://api.binance.com/api/v3/account

https://api.binance.com/api/v3/order

you get (proper) error message:

{"code":-2014,"msg":"API-key format invalid."}

but when i try to do it from the Webrequest "https://api.binance.com/api/v3/order" returns this message, but "https://api.binance.com/api/v3/account" returns 404.

Of course I added https://api.binance.com/api/v3 to allowed URLs.

I distilled the essence of the code, so one may just paste this as an expert advisor:

#property copyright "Copyright 2018,Grzegorz Korycki"
#property version   "1.00"
#property strict

//add https://api.binance.com/api/v3 to allowed addresses

void Send2()
  {
   char result_char[]; string result_headers; char url_char[];

   string url = "https://api.binance.com/api/v3/order";
   StringToCharArray(url, url_char, 0, WHOLE_ARRAY, CP_UTF8);
   ArrayResize(url_char, ArraySize(url_char)-1);
   ResetLastError();
   Alert("Executing: "+url);
   int res=WebRequest("POST",url,"","",1000,url_char,ArraySize(url_char),result_char, result_headers);
   string result1 = CharArrayToString(result_char,0,WHOLE_ARRAY);
   if(res!=200)
   {
      Alert("Authorization error #"+(string)res+", LastError="+(string)GetLastError());
      Alert("result1 = "+ result1);
   }
   
   url = "https://api.binance.com/api/v3/account";
   StringToCharArray(url, url_char, 0, WHOLE_ARRAY, CP_UTF8);
   ArrayResize(url_char, ArraySize(url_char)-1);
   ResetLastError();
   Alert("Executing: "+url);
   res=WebRequest("POST",url,"","",1000,url_char,ArraySize(url_char),result_char, result_headers);
   result1 = CharArrayToString(result_char,0,WHOLE_ARRAY);
   if(res!=200)
   {
      Alert("Authorization error #"+(string)res+", LastError="+(string)GetLastError());
      Alert("result1 = "+ result1);
   }
   
  }

int OnInit()
  {
   Send2();      
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }

What am I missing? Why the same exact method returns 2 different results (while browsers return same results)?

 
Check if it's not the same problem
WebRequest to download ForexFactory News
WebRequest to download ForexFactory News
  • 2017.03.06
  • www.mql5.com
Hi ‌My code to download FF news don't work anymore since this WE ! Anyone using webrequest to download news has this problem...
 
Alain Verleyen:
Check if it's not the same problem

thank you, but it seems not to be the case.

I added header

string reqheaders="User-Agent: Mozilla/4.0\r\n";
[...]
url = "https://api.binance.com/api/v3/account";
[...]
res=WebRequest("POST",url,reqheaders,"",1000,url_char,ArraySize(url_char),result_char, result_headers);

but the result is still the same - 404 error, the same as you would get by entering incorrect address in browser like "https://api.binance.com/api/v3/account2"

I also noticed that all other addresses can't be found: (and few others)

   url = "https://api.binance.com/api/v1/exchangeInfo";
   url = "https://api.binance.com/api/v1/ping";

the only one that seems to be working is /order/

 

Order end point is Signed, so you should send your api key like this:

   string headers = "X-MBX-APIKEY: " + apiKey ;

this change solves this error:

{"code":-2014,"msg":"API-key format invalid."}

 
Grzegorz Korycki:

thank you, but it seems not to be the case.

I added header

but the result is still the same - 404 error, the same as you would get by entering incorrect address in browser like "https://api.binance.com/api/v3/account2"

I also noticed that all other addresses can't be found: (and few others)

the only one that seems to be working is /order/

"https://api.binance.com/api/v1/ping
Avail. only for GET method
Reason: