8

If I have a POST parameter of

d={"data": "<span>hello</span>"}

which is a JSON string and it works fine and request.POST.get('d') contains the full string. But if I change it to

d = {"data": "<span>hel;lo</span>"}
print (request.POST.get('d')) #prints '{"data": "<span>hel'

For some reason anything after a semicolon is cut off. I can confirm this is not Javascript doing this because I used to use the exact same javascript code to post to a PHP API which was able to retrieve the data. Since moving to Python and webapp2 I've had this issue.

Michael Bates
  • 1,884
  • 2
  • 29
  • 40

2 Answers2

3

Run your string through encodeURIComponent(). Then components that truncate would be encoded. Afterwards when retrieving the data you need to decode.

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
1

This depends on the Content-Type of the request. If content type is application/x-www-form-urlencoded then you need to urlencode the params. See first answer for a detailed explanation: application/x-www-form-urlencoded or multipart/form-data?

Community
  • 1
  • 1
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • This confuses me as to why I would need to do this. It worked fine when I was using a PHP API (with the exact same $.ajax() call). I'll try what you suggested though. – Michael Bates Nov 15 '12 at 09:57