Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response No JSON object could be decoded #14

Open
javierdelgado1 opened this issue Jan 31, 2017 · 2 comments
Open

Response No JSON object could be decoded #14

javierdelgado1 opened this issue Jan 31, 2017 · 2 comments

Comments

@javierdelgado1
Copy link

javierdelgado1 commented Jan 31, 2017

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
Copy link
Contributor

elias19r commented Feb 6, 2017

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
Copy link

I have the same issue using Refit for C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants