Ok this is a tricky one, but I think I'm missing something really simple here.
I'm trying to create an interface to Trac from another domain using the XmlRpcPlugin's JSON interface to the Trac API. I'm using a PHP server-side script as a proxy because of Javascript's Same Origin Policy.
The proxy appears to be (mostly) working because I do get a response back from Trac in the jQuery-driven interface. However, I always get this response:
{
"error": {
"message": "JsonProtocolException details : No JSON object could be decoded",
"code": -32700,
"name": "JSONRPCError"
},
"result": null,
"id": null
}
These are the two entries in my Trac log that correspond with every request:
2012-03-05 11:37:55,943 Trac[json_rpc] ERROR: RPC(json) decode error
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/json_rpc.py", line 148, in parse_rpc_request
data = json.load(req, cls=TracRpcJSONDecoder)
File "/usr/lib/python2.6/json/__init__.py", line 267, in load
parse_constant=parse_constant, **kw)
File "/usr/lib/python2.6/json/__init__.py", line 318, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/json_rpc.py", line 99, in decode
obj = json.JSONDecoder.decode(self, obj, *args, **kwargs)
File "/usr/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
2012-03-05 11:37:55,943 Trac[web_ui] ERROR: RPC(JSON-RPC) Error
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/web_ui.py", line 143, in _rpc_process
rpcreq = req.rpc = protocol.parse_rpc_request(req, content_type)
File "/usr/local/lib/python2.6/dist-packages/TracXMLRPC-1.1.2_r11148-py2.6.egg/tracrpc/json_rpc.py", line 162, in parse_rpc_request
raise JsonProtocolException(e, -32700)
JsonProtocolException: No JSON object could be decoded
My jQuery code:
$.ajax({
url: "/ajax/trac_proxy.php",
dataType: "json",
data: {"method": "system.listMethods", "id": 1},
timeout: 5000,
type: 'POST',
success: function(data, status, XHR){
alert(JSON.stringify(data));
}
});
My PHP script (/ajax/trac_proxy.php
- shortened for clarity):
<?php
$cparams = array(
'http' => array(
'method' => 'POST',
'ignore_errors' => true,
'content' => http_build_query($_POST),
'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
. "Content-Type: application/json\r\n"
)
);
$context = stream_context_create($cparams);
$fp = fopen('https://mytracdomain.com/login/jsonrpc', 'rb', false, $context);
echo stream_get_contents($fp);
?>
To see what PHP is getting/doing, I changed /ajax/trac_proxy.php
to this:
$cparams = array(
'http' => array(
'content' => http_build_query($_POST),
'method' => 'POST',
'ignore_errors' => true,
'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
. "Content-Type: application/json\r\n"
)
);
var_dump($_POST);
var_dump(http_build_query($_POST));
var_dump($cparams);
This is what I get back:
array(2) {
["method"]=>
string(18) "system.listMethods"
["id"]=>
string(1) "1"
}
string(30) "method=system.listMethods&id=1"
array(1) {
["http"]=>
array(4) {
["content"]=>
string(30) "method=system.listMethods&id=1"
["method"]=>
string(4) "POST"
["ignore_errors"]=>
bool(true)
["header"]=>
string(79) "Authorization: Basic <REMOVED>
Content-Type: application/json
"
}
}
Trac 0.11.7
Python 2.6
Apache 2.2.14
Ubuntu 10.04
What am I doing wrong?
Figured it out.
The problem was the content
param in $cparams
.
http_build_query()
returns a URL query string. What the JSON-RPC plugin was expecting was a JSON string (imagine that), which json_encode()
handily provides.
Changing this:
$cparams = array(
'http' => array(
'content' => http_build_query($_POST),
'method' => 'POST',
'ignore_errors' => true,
'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
. "Content-Type: application/json\r\n"
)
);
To this:
$cparams = array(
'http' => array(
'content' => json_encode($_POST),
'method' => 'POST',
'ignore_errors' => true,
'header' => "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n"
. "Content-Type: application/json\r\n"
)
);
Fixed it.