JSON, HTTP POST and Django - json contaminated with newlines

80 views
Skip to first unread message

Hannson

unread,
May 15, 2009, 3:04:29 PM5/15/09
to Google Web Toolkit
Hi all,

I'm having some issues with json, http post and django.

The issue is that the JSONObject.toString() or HTTP POST request
builder contaminates the json string with newlines causing malformed
javascript to be sent and a ValueError thrown in Djangos simplejson
parser on the other side.

Here's how I build the request:

if(this.key != null)
{
JSONObject form = new JSONObject();

form.put("key", new JSONString(this.key));
form.put("content", new JSONString(getDocumentContent()));
form.put("title", new JSONString(getDocumentTitle()));

try {
requestBuilder.sendRequest("json="+form.toString(), new JSONhandler
());
} catch (RequestException e) {
Window.alert(e.getMessage());
e.printStackTrace();
}
}

Here's how I process the request in Django:

def save_page(request):
if(request.method == 'GET'):
pass
else:
json = request.POST['json']

decoder = simplejson.JSONDecoder()
props = decoder.decode(json)

try:
page_id = 1
page_alias = "Start"

m_page = Page.objects.get(pk=page_id)
except Page.DoesNotExist:

m_page = Page(alias=page_alias, rev=0)
m_page.save()

title = props['title']
content = props['content']
title = "test"
content=json
revision = Revisions(page=m_page, rev=m_page.rev+1,
title=title, content=content)
revision.save()

m_page.rev = m_page.rev + 1
m_page.save()

return render_to_response("it works")




I've determined that the newlines most likely added in the HTTP
request builder, but I haven't been able to debug it.

Has anyone here encountered and solved this problem before?

Hannson

unread,
May 15, 2009, 4:14:22 PM5/15/09
to Google Web Toolkit
I think I was wrong.

This problem does not happen unless the json string contains quotes
(escaped automatically by GWT like this: "this is a string that
contains \"quotes\"" ), even when URL.encoded. I'm not sure if this is
a GWT or Django issue.

Does anyone have any ideas?

Thomas Broyer

unread,
May 15, 2009, 6:12:16 PM5/15/09
to Google Web Toolkit
Have you tried URL.encodeComponent() on your JSON before sendRequest
("json=" + json, ...) ?

I believe GWT is right on both JSON serialization and
URL.encodeComponent() (this one is deferred to the browser);
RequestBuilder is OK too (and let's hope your browser's XMLHttpRequest
too).
So I'd say that either your code is buggy, or PHP, or Django (PHP
being buggy in many ways isn't a scoop). You should first have a look
at the request's content that goes on the wire (use Firebug or a
similar tool, or Fiddler or similar network "debugger"), and if it
looks OK, then the problem is on the server side.

Hannson

unread,
May 15, 2009, 7:31:44 PM5/15/09
to Google Web Toolkit
The issue was fixed by encoding the JSON with URL.encodeComponent().

I'm very thankful for your help!
Reply all
Reply to author
Forward
0 new messages