0

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?

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
Derrick
  • 336
  • 5
  • 18

1 Answers1

0

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())
Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Right, that's been tried. As you can see from the 4 examples cited in the question, the second and third example both make use of double-quotes. Other ideas? – Derrick Oct 14 '12 at 23:26
  • @Derrick Sorry, didn't see that. The first and fourth example can never ever work, so they're indeed irrelevant to the problem at hand. Updated the answer with another likely culprit. – phihag Oct 15 '12 at 09:25
  • Ah, you know what? Your suggestion above caused me to verify that the client was actually sending the HTTP headers - it wasn't. So I updated the client to actually do what was expected of it (send those headers) and it's working as expected. Thanks for your insight. – Derrick Oct 15 '12 at 14:18