1

This unanswered question is asked by someone else. And I do encounter the same exact question with the same exact scenario.

I tried to query a Stellar API which works perfectly fine using the curl command line. But it somehow return "Unable to parse request" when the query is made by PHP/Magento. I have tried to use all various methods to make the client request including curl_init, Varien_Http_Client, and all return the same message.

After some time, I realize that the error is not necessarily magento error message. It could be the error message returned by stellar.

But even though it is stellar error, how can we create the same exact POST request between curl and PHP.

curl -X POST https://test.stellar.org:9002 -d '
{
     "method": "account_currencies",
     "params": [
          {"account": "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U"}
     ]
}'

PHP Code:

<?php
$method = "account_currencies";
$params = Array("account" => "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U");
$request = xmlrpc_encode_request($method, $params);
$ch = curl_init();
$headers[] = "Content-Type: text/xml" ;
$headers[] = "Content-length: ".strlen($request) . "rn";
curl_setopt($ch, CURLOPT_URL, "https://test.stellar.org");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PORT , 9002);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
$response = curl_exec($ch);
curl_close($ch);
echo $response;

Clue 1:

I suspect that the error came from Stellar when the following condition executed.

if ((request.size () > 1000000) ||
    ! reader.parse (request, jvRequest) ||
    jvRequest.isNull () ||
    ! jvRequest.isObject ())
{
    return createResponse (400, "Unable to parse request");
}

Clue 2

The error only occur when the json is invalid. could be because of any newline character or escaped character.

Bad Day with bad Question :(

Please ignore this question. Apparently it is indeed an invalid json was created in the POST data.

For my case, I manage to create the POST data using the following code:

<?php

$request_data = array(
    "method" => "account_currencies",
    "params" => array(
        array(
            "account" => "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U"
        )
    )
);

$client = new Varien_Http_Client($url);
$client->setMethod(Varien_Http_Client::POST);
$client->setRawData(json_encode($request_data), 'application/json');
$client->request();
0

Apparently it is indeed an invalid json was created in the POST data.

For my case, I manage to create the POST data using the following code:

<?php

$request_data = array(
    "method" => "account_currencies",
    "params" => array(
        array(
            "account" => "gM4Fpv2QuHY4knJsQyYGKEHFGw3eMBwc1U"
        )
    )
);

$client = new Varien_Http_Client($url);
$client->setMethod(Varien_Http_Client::POST);
$client->setRawData(json_encode($request_data), 'application/json');
$client->request();

Not the answer you're looking for? Browse other questions tagged or ask your own question.