0

Localhost used is macOS 10.13 python 3.6 django 2.0.2

windowServer2012R2 used django 2.0.2, python 3.4, iis,fastCGI

If I run localhost, this screenshot I have:

enter image description here

And putting data and post return 200 connect success:

enter image description here

But CSS is not working on the window Server.

This screenshot:

enter image description here

And put data and post produce:

exception the JSON object must be str, not 'bytes' in json.loads(request.body)

Django debug Post data

_content_type   
'application/json'
_content    
('{\r\n'
 '    "UserEmail": "",\r\n'
 '    "UserPassword": "",\r\n'
 '    "UserSex": 1,\r\n'
 '    "UserAge": 1,\r\n'
 '    "DeviceId": "",\r\n'
 '    "PushKey": "",\r\n'
 '    "OS": 1,\r\n'
 '    "OSVersion": ""\r\n'
 '}')

enter image description here

I think its an encoding problem, but I can't solve it.

localhost

print(request.body)

result

`b'{\n    "UserEmail": "",\n    "UserPassword": "",\n    "UserSex": 1,\n    "UserAge": 1,\n    "DeviceId": "",\n    "PushKey": "",\n    "OS": 1,\n    "OSVersion": ""\n}'`

windowserver

`b'_content_type=application%2Fjson&_content=%7B%0D%0A++++%22UserEmail%22%3A+%22%22%2C%0D%0A++++%22UserPassword%22%3A+%22%22%2C%0D%0A++++%22UserSex%22%3A+1%2C%0D%0A++++%22UserAge%22%3A+1%2C%0D%0A++++%22DeviceId%22%3A+%22%22%2C%0D%0A++++%22PushKey%22%3A+%22%22%2C%0D%0A++++%22OS%22%3A+1%2C%0D%0A++++%22OSVersion%22%3A+%22%22%0D%0A%7D'`

I tried json.loads(request.body.decode("utf-8")) exception this enter image description here

1 Answer 1

0

try below code

import json

body_string = request.body.decode("utf-8", "ignore")
data = json.loads(body_string)

or just try like

data = json.loads(request.data.get('_content'))
4
  • same exception of last screenshot at post
    – Adam
    Apr 25, 2018 at 8:13
  • @Adam try like data = request.json() Apr 25, 2018 at 8:18
  • exception 'Request' object has no attribute 'json' so i tried request.data print(request.data) <QueryDict: {'_content': ['{\r\n "UserEmail": "",\r\n "UserPassword": "",\r\n "UserSex": 1,\r\n "UserAge": 1,\r\n "DeviceId": "",\r\n "PushKey": "",\r\n "OS": 1,\r\n "OSVersion": ""\r\n}'], '_content_type': ['application/json']}> but json.loads(request.data) exception the JSON object must be str, not 'QueryDict'
    – Adam
    Apr 25, 2018 at 8:25
  • @Adam oh! then try data = json.loads(request.data.get('_content')) Apr 25, 2018 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.