blinktrade / BlinkTradeJS

BlinkTrade JavaScript SDK and CLI
GNU General Public License v3.0
122 stars 46 forks source link

Response No JSON object could be decoded #14

Open javierdelgado1 opened 7 years ago

javierdelgado1 commented 7 years ago

Hello guys, I am trying to make a request, but I have not had any success, I always send my fix in Json format but I always get the same answer, how can I solve this?

The small code is written in php using the laravel framework 5.4.3

    $nonce = strval(time());

$signature = hash_hmac("sha256", $nonce, $this->secret); $array = [ "MsgType" => "U2", "BalanceReqID" => 1, ]; $message = json_encode($array); $client = new \GuzzleHttp\Client(array( "curl" => array(CURLOPT_SSL_VERIFYPEER => false))); $response = $client->request( "POST", "https://api.testnet.blinktrade.com/tapi/v1/message", [ 'headers' => [ "User-Agent" => "testing/1.0", "Content-Type" => "application/json", "Nonce" => $nonce, "Signature" => $signature, "APIKey" => $this->key ] ], $message

    );
    $response = json_decode($response->getBody()->getContents());
    return  response()->json($response);   

Thank you very much for your help

elias19r commented 7 years ago

Hi,

I tried your code and I got "No JSON object could be decoded" as well.

I still don't know the reason, but I suspect it is something related to GuzzleHttp, because I managed to get a success response using pure curl as follows:

<?php
// @param {array} $message
function send($message)
{
    $message = json_encode($message);

    // Trade API URL
    $url = 'https://api.testnet.blinktrade.com/tapi/v1/message';

    // Set API Key and Secret
    $key    = 'MvkISyWHJN1vCqJtpRkc35l30U7X9wZaiTJhj6nOIlQ';
    $secret = '7DWNrwycvs2jgDYn6SPrQDuAz1qIlsrHAmgKhB2UeJE';

    // Generate a nonce
    $nonce = strval(time());

    // Sign the nonce with secret using HMAC-SHA256
    $signature = hash_hmac('sha256', $nonce, $secret);

    // Define headers
    $headers = [
        'APIKey:' . $key,
        'Nonce:' . $nonce,
        'Signature:' . $signature,
        'Content-Type:application/json'
    ];

    // Send a POST message to API URL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

$response = send([
    'MsgType'      => 'U2',
    'BalanceReqID' => 1
]);

print_r(json_decode($response, true));

Output:

Array
(
    [Status] => 200
    [Description] => OK
    [Responses] => Array
        (
            [0] => Array
                (
                    [MsgType] => U3
                    [5] => Array
                        (
                            [BTC_locked] => 0
                            [USD] => 2196900989710
                            [BTC] => 7449757495
                            [USD_locked] => 0
                        )
                    [ClientID] => 90800558
                    [BalanceReqID] => 1
                )
        )
)
abraaocaldas commented 6 years ago

I have the same issue using Refit for C#.