Is it possible to customize HTTP request headers with WebRequest?

 
I am relying on the new MQL5's function WebRequest in order to consume a RESTful webservice from an EA, however I can't POST some JSON data this way:

string headers;
char result[];
string signal = "{\"ea_id\": 1,\"symbol\": \"AUDUSD\",\"operation\": \"BUY\",\"value\": 0.9281}";
StringToCharArray(signal,data);

WebRequest("POST","http://api.laplacianlab.local/signal/add",NULL,NULL,50,data,ArraySize(data),result,headers);

Because WebRequest's POST assumes that you should post application/x-www-form-urlencoded data.

I would like my request HTTP headers to have these fields:

Content-Type: application/json
Accept: application/json

instead of:

Content-Type: application/x-www-form-urlencoded
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

Shouldn't WebRequest allow programmers to specify all HTTP header request fields? IMHO, it makes more sense somethink like the following:
WebRequest("POST",uri,httpReqHeader,50,data,ArraySize(data),result,headers);
Anyway, how would you solve this? Would you build a webservice ready to receive form-urlencoded data from an MQL5 client and then convert to JSON?

The only alternative I see is building a new HTTP component from scratch dealing manually with wininet, but on the other hand I think it might break at any moment because of wininet issues.

Any feedback is welcome, thank you in advance!
 
laplacianlab:
I am relying on the new MQL5's function WebRequest in order to consume a RESTful webservice from an EA, however I can't POST some JSON data this way:

Because WebRequest's POST assumes that you should post application/x-www-form-urlencoded data.

I would like my request HTTP headers to have these fields:

Content-Type: application/json
Accept: application/json

instead of:

Content-Type: application/x-www-form-urlencoded
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

Shouldn't WebRequest allow programmers to specify all HTTP header request fields? IMHO, it makes more sense somethink like the following:
Anyway, how would you solve this? Would you build a webservice ready to receive form-urlencoded data from an MQL5 client and then convert to JSON?

The only alternative I see is building a new HTTP component from scratch dealing manually with wininet, but on the other hand I think it might break at any moment because of wininet issues.

Any feedback is welcome, thank you in advance!

Hi Jordi, in my opinion WebRequest is the first and very relevant step of a MT5 native HTTP function. Period.

And this was really necessary because several reasons, like standard and security, and, of course, because a good and safe HTTP access at an EA is very relevant.

I see your ideas to consume a RESTful webservice from an EA as one of the next steps, and I think while we don't have this we may create workarounds, like build a Webservice ready to receive urlencoded data, as you pointed out.

 
figurelli:

Hi Jordi, in my opinion WebRequest is the first and very relevant step of a MT5 native HTTP function. Period.

And this was really necessary because several reasons, like standard and security, and, of course, because a good and safe HTTP access at an EA is very relevant.

I see your ideas to consume a RESTful webservice from an EA as one of the next steps, and I think while we don't have this we may create workarounds, like build a Webservice ready to receive urlencoded data, as you pointed out.

Thank you figurelli, I will use the API provided by MetaQuotes.
 
laplacianlab:
Thank you figurelli, I will use the API provided by MetaQuotes.
You are welcome.
 

Hi!

I second that wish of Jordi. The web service I want to call requires a token based header authentication. Without being able to set the request headers I cannot use the WebRequest method. Doomed to use WinINET for now... 

 
I suggest you to write to ServiceDesk.
 

Try to add headers using referrer argument.

<your referrer>+"\r\nContent-Type: application/json\r\nAccept: application/json"

 
alexvd:

Try to add headers using referrer argument.

<your referrer>+"\r\nContent-Type: application/json\r\nAccept: application/json"

There seems to be a bug in my version - Build 4.00.670

When I try to enter an ip address and port in the URL, I get a 4051 error.

Try:

http://google.com (OK)

http://google.com:80 (ERR 4051)

http://173.194.40.167 (ERR 4051)

http://173.194.40.167:80 (ERR 4051)

Google
Google
  • www.google.ru
Голосовой поиск – это очень удобно! Чтобы найти нужную информацию, скажите "О'кей Google" и произнесите запрос.
 
alexvd:

Try to add headers using referrer argument.

<your referrer>+"\r\nContent-Type: application/json\r\nAccept: application/json"

btw, it looks like the application/x-www-form-urlencoded content type is written after the referrer bit:

 

   string body = "{\"data\":\"123\"}";

   StringToCharArray(body ,data);

   int res = WebRequest( 

               "POST", 

               "http://macmini/price",

               cookie, 

               "http://blah/\r\nContent-Type: application/json\r\nAccept: application/json", 

               TIMEOUT,

               data, 

               ArraySize(data), 

               result, 

               headers); 

 

And on the NodeJS side:

POST /price 200 1ms - 36b

Headers: { 'cache-control': 'no-cache',

  connection: 'Keep-Alive',

  pragma: 'no-cache',

  'content-type': 'application/x-www-form-urlencoded',

  accept: 'application/json',

  'accept-charset': '*,utf-8;',

  'accept-language': 'en',

  host: 'macmini',

  referer: 'http://blah/',

  'user-agent': 'MetaTrader 4 Terminal/4.670 (Windows NT 6.1; x86)',

  'proxy-connection': 'Keep-Alive',

  'content-length': '15' }

Body: { '{"data":"123"}\u0000': '' }

 
jseppa:

btw, it looks like the application/x-www-form-urlencoded content type is written after the referrer bit:

 

   string body = "{\"data\":\"123\"}";

   StringToCharArray(body ,data);

   int res = WebRequest( 

               "POST", 

               "http://macmini/price",

               cookie, 

               "http://blah/\r\nContent-Type: application/json\r\nAccept: application/json", 

               TIMEOUT,

               data, 

               ArraySize(data), 

               result, 

               headers); 

 

And on the NodeJS side:

POST /price 200 1ms - 36b

Headers: { 'cache-control': 'no-cache',

  connection: 'Keep-Alive',

  pragma: 'no-cache',

  'content-type': 'application/x-www-form-urlencoded',

  accept: 'application/json',

  'accept-charset': '*,utf-8;',

  'accept-language': 'en',

  host: 'macmini',

  referer: 'http://blah/',

  'user-agent': 'MetaTrader 4 Terminal/4.670 (Windows NT 6.1; x86)',

  'proxy-connection': 'Keep-Alive',

  'content-length': '15' }

Body: { '{"data":"123"}\u0000': '' }

I have trying to set the user-agent in the request header, but apparently I don't get any relevant response from server .....

Is it possible to set user agent in request's header? What is the default user agent? 

Please let me know.

 
Jordi Bassaganas:
I am relying on the new MQL5's function WebRequest in order to consume a RESTful webservice from an EA, however I can't POST some JSON data this way:

Because WebRequest's POST assumes that you should post application/x-www-form-urlencoded data.

I would like my request HTTP headers to have these fields:

Content-Type: application/json
Accept: application/json

instead of:

Content-Type: application/x-www-form-urlencoded
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*

Shouldn't WebRequest allow programmers to specify all HTTP header request fields? IMHO, it makes more sense somethink like the following:
Anyway, how would you solve this? Would you build a webservice ready to receive form-urlencoded data from an MQL5 client and then convert to JSON?

The only alternative I see is building a new HTTP component from scratch dealing manually with wininet, but on the other hand I think it might break at any moment because of wininet issues.

Any feedback is welcome, thank you in advance!


Try to append a slash ("/") on the URL:


WebRequest("POST","http://api.laplacianlab.local/signal/add/",NULL,NULL,50,data,ArraySize(data),result,headers);
Reason: