BlinkTradeJS icon indicating copy to clipboard operation
BlinkTradeJS copied to clipboard

Response No JSON object could be decoded

Open javierdelgado1 opened this issue 8 years ago • 2 comments

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

javierdelgado1 avatar Jan 31 '17 04:01 javierdelgado1

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
                )
        )
)

elias19r avatar Feb 06 '17 14:02 elias19r

I have the same issue using Refit for C#.

abraaocaldas avatar Nov 14 '17 03:11 abraaocaldas