سؤال

I have a strange issue with the TCP JSON-RPC server I've created in Python-2.7. I used the following code to build the server:

https://github.com/joshmarshall/jsonrpclib

I am communicating client to server from within the same local network. In the console window, I can connect to and run commands against the server from within Python. All is well there.

However, when I try to send JSON strings from a mobile app (in this case an iPad) I get an error on the server. I have also downloaded this tool in an attempt to send the JSON strings: http://www.simplecomtools.com/productcart/pc/downloads/tcptesttool.zip but with the same error result. The server is reporting a "Bad request syntax" error. I've tried several different strings - the displayed errors are:

192.168.1.107 - - [13/Oct/2012 09:48:17] code 400, message Bad request syntax ("{'jsonrpc':'2.0','method':'add','params':[3,6],'id':'8'}") 192.168.1.107 - - [13/Oct/2012 09:48:17] "{'jsonrpc':'2.0','method':'add','params':[3,6],'id':'8'}" 400 -

192.168.1.107 - - [13/Oct/2012 09:49:44] code 400, message Bad request syntax ('{"jsonrpc":"2.0","method":"add","params":[3,6],"id":"8"}') 192.168.1.107 - - [13/Oct/2012 09:49:44] "{"jsonrpc":"2.0","method":"add","params":[3,6],"id":"8"}" 400 -

192.168.1.107 - - [13/Oct/2012 09:50:49] code 400, message Bad request syntax ('{"jsonrpc":"2.0","method":"add","params":{"x":3,"y":6},"id":"8"}') 192.168.1.107 - - [13/Oct/2012 09:50:49] "{"jsonrpc":"2.0","method":"add","params":{"x":3,"y":6},"id":"8"}" 400 -

192.168.1.107 - - [13/Oct/2012 17:11:59] code 400, message Bad request syntax ("{'jsonrpc':'2.0', 'method':'add', 'params':{'x':3,'y':6}, 'id':8}") 192.168.1.107 - - [13/Oct/2012 17:11:59] "{'jsonrpc':'2.0', 'method':'add', 'params':{'x':3,'y':6}, 'id':8}" 400 -

I really have no idea why the server would think the request syntax is bad, and I feel a little silly even asking the question. Any ideas on what I could try to solve the syntax error?

هل كانت مفيدة؟

المحلول

In message 1 and 4, your client is not actually sending JSON; it is using ' to denote string boundaries, instead of ". While single quotes are supported by some JSON implementations, they are invalid according to the standard. Correct your client implementation to send actual JSON with "-delimited strings.

But the main problem is that you're not wrapping your messages into HTTP POST requests, but sending them raw. A proper JSONRPC request looks like:

POST / HTTP/1.0
Content-Length: 71

{"jsonrpc": "2.0", "params": [3, 6], "id": "er5qtdbz", "method": "pow"}

, but you're sending just the last line.

In Python, you can send a valid request with the following example program:

import json
try:
    from urllib.request import urlopen
except ImportError: # Python<3
    from urllib2 import urlopen

req = {"jsonrpc":"2.0","method":"add","params":[3,6],"id":0}
req_data = json.dumps(req).encode('utf-8')
u = urlopen('http://localhost:8080/', req_data)
print(u.read())
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top