6 registered members (TipmyPip, Niels, dBc, Ed_Love, 3run, 1 invisible),
17,916
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
http functions to IG REST API
#466290
06/06/17 09:53
06/06/17 09:53
|
Joined: Aug 2016
Posts: 27
MaskOfZorro
OP
Newbie
|
OP
Newbie
Joined: Aug 2016
Posts: 27
|
I'm trying to send a POST request to IG's REST API to log in and retrieve a token for trading. The required format of the request is Request Section Details Action POST https://demo-api.ig.com/gateway/deal/sessionHeader Content-Type: application/json; charset=UTF-8 Accept: application/json; charset=UTF-8 VERSION: 2 X-IG-API-KEY: 5FA056D2706634F2B7C6FC66FE17517B Body { "identifier": "A12345", "password": "112233" } I already have a functioning MQL4 code that accomplishes this, but now I've tried to adapt it to Zorro and can't get it to work. Here is the relevant poriton of the Zorro script. The first part is mine and the part with the codnitionals just taken from the example in the manual. (Please note escape characters aren't appearing the in the quotations, e.g. bodystring is actually ""{BACKSLASH"identifierBACKSLASH" etc.) char inputbody[100]; char content[1000]; string url = "https://demo-api.ig.com/gateway/deal/session"; string bodystring = "{"identifier": "luke1234", "password": "One23456."}"; strcpy (inputbody, bodystring); string inputheader = "Content-Type: application/json; charset=UTF-8 rn Accept: application/json; charset=UTF-8 rn X-IG-API-KEY: 6ee4e97eca7d1dc5afa51a8f8028f3cff725bb27"; int id = http_send (url, inputbody, inputheader); while(!http_status(id)) { if(!wait(100)) return; // wait for the server to reply printf("."); } if(http_status(id) > 0) { //transfer successful? http_result(id,content,1000); //get the replied IP printf("n%s",content);
} else printf("nError during transfer!");
http_free(id); The API key etc. are all correct, as these work in Metatrader 4. But printf is producing the following: <!--[if IE 7]><html class="ie7 nojs" lang=""> <![endif]--> <!--[if IE 8]><html class="ie8 nojs" lang=""> <![endif]--> <!--[if IE 9]><html class="ie9 nojs" lang=""> <![endif]--> <html class="gtie9" lang=""><!--<![endif]--> <head> <title>403</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1; requiresActiveX=true"> <meta property="og:title" content="403 Access forbidden!"> <meta property="og:url" c <meta itemprop="url" content="/error/403"> <meta property="og:description" content="Access forbidden!"> <meta property="og:image" content="/error/include/og_image.jpg"> <meta name="keywords" content=""> <meta name="locale" id="locale" content="en_GB"> <meta name="softid" id="softid" content="igcom"> <meta name="apple-itunes-app" content="app-id=469403477"> <link rel="stylesheet" href="/error/include/errorpage.css" type="text/ Any idea what's wrong? Is this something to do with the JSON format the body of the POST request? Is there anyw ay to do this? The MQL4 code is pretty straightforward: string responseheader; char responsebody[]; string inputheader = "Content-Type: application/json; charset=UTF-8 rn Accept: application/json; charset=UTF-8 rn X-IG-API-KEY: 6ee4e97eca7d1dc5afa51a8f8028f3cff725bb27"; string bodystring = "{"identifier": "luke1234", "password": "One23456"}"; char inputbody[]; StringToCharArray(bodystring, inputbody);
string url = "https://demo-api.ig.com/gateway/deal/session";
int request = WebRequest("POST", url, inputheader, 50, inputbody, responsebody, responseheader);
Last edited by MaskOfZorro; 06/06/17 09:58.
|
|
|
Re: http functions to IG REST API
[Re: MaskOfZorro]
#466358
06/11/17 20:31
06/11/17 20:31
|
Joined: Feb 2017
Posts: 369
Dalla
Senior Member
|
Senior Member
Joined: Feb 2017
Posts: 369
|
Can you try using Wireshark or similar tool to make sure a post request is generated? To be honest it looks like a get request. The error page you get as a response is what you get also if you put that address in your browser https://demo-api.ig.com/gateway/deal/sessionOn the other hand, if I try a POST with e.g. invalid credentials, I get a 400 BAD REQUEST with a JSON body as response. { "errorCode": "error.security.invalid-details" }
|
|
|
Re: http functions to IG REST API
[Re: Dalla]
#466390
06/12/17 20:00
06/12/17 20:00
|
Joined: Aug 2016
Posts: 27
MaskOfZorro
OP
Newbie
|
OP
Newbie
Joined: Aug 2016
Posts: 27
|
Can you try using Wireshark or similar tool to make sure a post request is generated? To be honest it looks like a get request. The error page you get as a response is what you get also if you put that address in your browser https://demo-api.ig.com/gateway/deal/sessionOn the other hand, if I try a POST with e.g. invalid credentials, I get a 400 BAD REQUEST with a JSON body as response. { "errorCode": "error.security.invalid-details" } Yes, seems to be a POST request. - Http: Request, POST /gateway/deal/session Command: POST - URI: /gateway/deal/session Location: /gateway/deal/session ProtocolVersion: HTTP/1.1 Host: demo-api.ig.com Accept: */* - ContentType: application/json; charset=UTF-8 + MediaType: application/json; charset=UTF-8 Accept: application/json; charset=UTF-8 VERSION: 2 X-IG-API-KEY: e6329bb0b1e25e9bb3b57c4cd3070a2a95a60d7f ContentLength: 52 HeaderEnd: CRLF - payload: HttpContentType = application/json; charset=UTF-8 HTTPPayloadLine: {"identifier": "xxx", "password": "xxx"}
|
|
|
Re: http functions to IG REST API
[Re: jcl]
#466391
06/12/17 20:16
06/12/17 20:16
|
Joined: Aug 2016
Posts: 27
MaskOfZorro
OP
Newbie
|
OP
Newbie
Joined: Aug 2016
Posts: 27
|
Try removing all stuff from the header except for "X-IG-API-KEY: 6ee4e97eca7d1dc5afa51a8f8028f3cff725bb27". As to my knowledge, the curl library sets "Content-Type: application/json" automatically for json data. When I use the full header I get the JSON result above in MS Network Monitor. If I remove everything but the key, it gives me - ContentType: application/x-www-form-urlencoded MediaType: application/x-www-form-urlencoded HeaderEnd: CRLF payload: HttpContentType = application/x-www-form-urlencoded and Zorro returns n{"errorCode":"Media type [mediaType=application/x-www-form-urlencoded] not supported."}
Interestingly, when I remove everything but the key but use an invalid key, I get this error message: n{"errorCode":"error.security.api-key-invalid"} But if I keep the full header and use an invalid key, I just get the same 403 result that I've been getting all along - it doesn't even pick up the invalid key. So the problem IS with the header? But what? Is the format of my header for the http function wrong?
Last edited by MaskOfZorro; 06/12/17 20:30.
|
|
|
Re: http functions to IG REST API
[Re: MaskOfZorro]
#466401
06/13/17 05:40
06/13/17 05:40
|
Joined: Feb 2017
Posts: 369
Dalla
Senior Member
|
Senior Member
Joined: Feb 2017
Posts: 369
|
One thing worth checking. In your http_send code, you have VERSION: 2 specified in your header. In your MQL4 code, you don't, which I think causes it to default to the latest version, i.e. VERSION: 3. Also looking at the specification for V2 vs V3, the input body for V3 also has a field for encryptedPassword, which is not there in V3. https://labs.ig.com/rest-trading-api-reference/service-detail?id=534
|
|
|
Re: http functions to IG REST API
[Re: MaskOfZorro]
#466406
06/13/17 07:30
06/13/17 07:30
|
Joined: Feb 2017
Posts: 369
Dalla
Senior Member
|
Senior Member
Joined: Feb 2017
Posts: 369
|
Seems then like the forward slash is an issue, although I don't know why. The first 20 chars in your header is Accept: application/
Try escaping the forward slash I guess? But like I said, I can't understand why that would be required...
Last edited by Dalla; 06/13/17 07:31.
|
|
|
|