Uri
Uri

Reputation: 26976

Appengine ProtoRPC can't decode my JSON

Can't seem to get protoRPC API on app-engine to work for me.

This is my request:

$.ajax({
    url: '/guestRPC.get_tags',
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: {
            prefix: JSON.stringify(request),
            locale: JSON.stringify('{{locale}}')
    },
    success: somefunction
});

This is what I'm sending according to the browser debugger:

Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,he;q=0.6
Connection:keep-alive
Content-Length:51
Content-Type:application/json
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.137 Safari/535.19
X-Requested-With:XMLHttpRequest
Request Payload
prefix=%7B%22term%22%3A%22%22%7D&locale=%22en_US%22
Response Headersview source
Cache-Control:no-cache
Content-Encoding:gzip
Content-Length:87
Date:Mon, 26 Mar 2012 18:58:24 GMT
Expires:Fri, 01 Jan 1990 00:00:00 GMT
Server:Google Frontend
Vary:Accept-Encoding
content-type:application/json
x-content-type-options:nosniff

And this is the error on server:

2012-03-26 21:56:02.161 /guestRPC.get_tags 500 152ms 0kb Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.137 Safari/535.19
- - - [26/Mar/2012:11:56:02 -0700] "POST /guestRPC.get_tags HTTP/1.1" 500 238 "mysite" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.137 Safari/535.19" "mysite" ms=152 cpu_ms=0 api_cpu_ms=0 cpm_usd=0.000138 pending_ms=86 instance=...
D2012-03-26 21:56:02.155
Entered guestRPC handler.
E2012-03-26 21:56:02.156
An unexpected error occured when handling RPC: No JSON object could be decoded: line 1 column 0 (char 0)
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/protorpc/webapp/service_handlers.py", line 601, in handle
    request = mapper.build_request(self, method_info.request_type)
  File "/base/python_runtime/python_lib/versions/1/protorpc/webapp/service_handlers.py", line 235, in build_request
    return self.__protocol.decode_message(request_type, handler.request.body)
  File "/base/python_runtime/python_lib/versions/1/protorpc/protojson.py", line 156, in decode_message
    dictionary = json.loads(encoded_message)
  File "/base/python_runtime/python_lib/versions/1/simplejson/__init__.py", line 388, in loads
    return _default_decoder.decode(s)
  File "/base/python_runtime/python_lib/versions/1/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/base/python_runtime/python_lib/versions/1/simplejson/decoder.py", line 420, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)
E2012-03-26 21:56:02.159
Internal Server Error

Upvotes: 0

Views: 766

Answers (2)

Rafe Kaplan
Rafe Kaplan

Reputation: 325

The problem that is happening here can be seen if you look at the "Request Payload" section. It turns out that when you use .ajax to send a request it will not put outer '{' and '}' in your request. Also, notice that the values are separated by an &. This means that .ajax turned your nice object in to a urlencoded request, which is not what you wanted.

The reason is that .ajax the "dataType" parameter only refers to how the .ajax function will handle the request content, not what it will do to the content when sending. .ajax always sends a query string. In order to get it to send json you must first convert the dictionary to a string.

Try using:

JSON.stringify({
        prefix: request,
        locale: '{{locale}}'
})

Upvotes: 3

Marc B
Marc B

Reputation: 360762

You're double-encoding your data, actually. Since you've indicated that the datetype is Json, all you have to do is provide a normal JS data structure:

data: {
        prefix: request,
        locale: '{{locale}}'
},

and jquery will take care of the stringification for you.

Upvotes: 0

Related Questions