1

I am uploading an image file:

oFReader.readAsDataURL(fileObject);

oFReader.onload = function(oFREvent){  
  var picture = oFReader.result; 
  (... continue)

And, ajaxing:

var dataDict = {'purpose':'newpicture', 'picture':picture};
$.ajax({
  type:'POST',
  url:'https://mydomain.whatever',
  data:JSON.stringify(dataDict)
})

When I print out 'picture' variable, it is like:

"data:image/jpeg;base64,/9j/4AAQSk...

Now, I am getting a new error on my AWS with Python 3.5, which did not occur on my local with Python 3.6. On line:

import json


if(request.body):
  data = json.loads(request.body)

I am getting the error:

TypeError: the JSON object must be str, not 'bytes'

I am guessing that it is because json.loads changed:

Changed in version 3.6: s can now be of type bytes or bytearray. The input encoding should be UTF-8, UTF-16 or UTF-32.

But, I thought that I was turning the image into base64 string. What am I sending? String or Bytes? How do I send this image as an string??

Martin Gergov
  • 1,556
  • 4
  • 20
  • 29
Heuyie
  • 87
  • 1
  • 12
  • Try using `request.get().json()` instead of `json.loads(request.body)` – Wamadahama Jul 18 '18 at 17:57
  • @Wamadahama Thanks for your response. I got error "AttributeError: 'WSGIRequest' object has no attribute 'get'" And this is Django project to be clear. – Heuyie Jul 18 '18 at 18:39
  • This might provide some insight on how to solve this: https://stackoverflow.com/questions/41006093/json-loads-and-redis-in-python-3-5 – Wamadahama Jul 18 '18 at 18:56
  • @Wamadahama OK! Take a look! Thanks again! :) – Heuyie Jul 18 '18 at 19:06

0 Answers0