Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
M1 Oversampling
by jcl. 04/26/24 11:12
Why Zorro supports up to 72 cores?
by jcl. 04/26/24 11:09
Eigenwerbung
by jcl. 04/26/24 11:08
MT5 bridge not working on MT5 v. 5 build 4160
by EternallyCurious. 04/25/24 20:49
Trading Journey
by howardR. 04/24/24 20:04
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, wandaluciaia, 1 invisible), 798 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
wandaluciaia, Mega_Rod, EternallyCurious, howardR, 11honza11
19049 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
http functions to IG REST API #466290
06/06/17 09:53
06/06/17 09:53
Joined: Aug 2016
Posts: 27
M
MaskOfZorro Offline OP
Newbie
MaskOfZorro  Offline OP
Newbie
M

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

Quote:
Request Section Details
Action POST https://demo-api.ig.com/gateway/deal/session
Header 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.)

Quote:
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:

Quote:
<!--[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:

Quote:

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
D
Dalla Offline
Senior Member
Dalla  Offline
Senior Member
D

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/session

On 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] #466375
06/12/17 08:58
06/12/17 08:58
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
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.

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
M
MaskOfZorro Offline OP
Newbie
MaskOfZorro  Offline OP
Newbie
M

Joined: Aug 2016
Posts: 27
Originally Posted By: Dalla
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/session

On 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
M
MaskOfZorro Offline OP
Newbie
MaskOfZorro  Offline OP
Newbie
M

Joined: Aug 2016
Posts: 27
Originally Posted By: jcl
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

Quote:
- ContentType: application/x-www-form-urlencoded
MediaType: application/x-www-form-urlencoded
HeaderEnd: CRLF
payload: HttpContentType = application/x-www-form-urlencoded


and Zorro returns
Quote:

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:

Quote:
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
D
Dalla Offline
Senior Member
Dalla  Offline
Senior Member
D

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: Dalla] #466404
06/13/17 06:20
06/13/17 06:20
Joined: Aug 2016
Posts: 27
M
MaskOfZorro Offline OP
Newbie
MaskOfZorro  Offline OP
Newbie
M

Joined: Aug 2016
Posts: 27
Yeah, I only added VERSION: 2 to see if that would help. Removing it or even explictly stating VERSION: 3 doesn't work.

I was wondering if curl had something to do with not all header fields going through, which is why it only recognises the invalid API key if I remove everything else ... they seem to be there in netmon but "Header Length" is 20? That doesn't seem right.

...

+ Versions: IPv4, Internet Protocol; Header Length = 20

...


Accept: application/json; charset=UTF-8
VERSION: 2
X-IG-API-KEY: e6329bb0b1e25e9bb3b57c4cd3070a2a95a60d7f
ContentLength: 52
HeaderEnd: CRLF

...

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
D
Dalla Offline
Senior Member
Dalla  Offline
Senior Member
D

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.
Re: http functions to IG REST API [Re: Dalla] #466412
06/13/17 09:18
06/13/17 09:18
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
I've checked what the curl library is doing. If the data string begins with '{', json is assumed, otherwise x-www-form-urlencoded. So in your case it should be json. Are you using the latest Zorro version? The curl library was updated in that version, maybe the previous version behaved differently.

Re: http functions to IG REST API [Re: jcl] #466540
06/20/17 08:25
06/20/17 08:25
Joined: Aug 2016
Posts: 27
M
MaskOfZorro Offline OP
Newbie
MaskOfZorro  Offline OP
Newbie
M

Joined: Aug 2016
Posts: 27
Great. This now works with the latest version.

However, http_result seems not to return the header of the response. How is this retrievable?


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1