Hi
I am really enjoying making web requests and doing async operations with vibe.d.
But now I need to make a web request given a description and PHP example(https://bittrex.com/home/api) :
For this version, we use a standard HMAC-SHA512 signing. >Append apikey and nonce to your request and calculate the >HMAC hash and include it under an apisign header. Note: the nonce is not respected right now but will be enforced later.
$apikey='xxx';
$apisecret='xxx';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/market/getopenorders?>apikey='.$apikey.'&nonce='.$nonce;
$sign=hashhmac('sha512',$uri,$apisecret);
$ch = curlinit($uri);
curlsetopt($ch, CURLOPTHTTPHEADER, >array('apisign:'.$sign));
$execResult = curlexec($ch);
$obj = jsondecode($execResult);
In my imagination I convert the example above to vibe.d like:
string apikey="xxx";
string apisecret="xxx";
string nonce = Clock.currTime().toString();
string url = "https://bittrex.com/api/v1.1/market/getopenorders?apikey=" ~ apikey ~ "&nonce=" ~ nonce;
auto hmac = HMAC!SHA512(apisecret.representation);
hmac.put(url.representation);
auto generatedHmac = hmac.finish();
char[] generatedHmacStr = cast(char[])generatedHmac[0..generatedHmac.length];
Json data;
try
{
requestHTTP(url.dup,
(scope req) {
req.method = HTTPMethod.GET;
req.headers["apisign"] = generatedHmacStr.idup;
},
(scope res) {
data = parseJsonString(res.bodyReader.readAllUTF8());
writeln(data);
}
);
}
catch ( std.json.JSONException e )
{
writeln("Exception was caught while authorization");
}
My code is not working and I am getting an exception like :
"Error: Expected valid JSON token, got '<!DOCTYPE HT'."
I am suspicious about three things :
1 - If string nonce = Clock.currTime().toString(); correct or not
if not how should I set the time?
2 - If char[] generatedHmacStr = cast(char[])generatedHmac[0..generatedHmac.length]; step correct or not
3 - If req.headers["apisign"] = generatedHmacStr.idup; step correct or not.
If you guys have exprience setting ubyte data to request header can you please answer my questions?
Best Regards
Erdem