from django.views.generic import View
from json import loads
from bson.json_util import dumps
from django.http import HttpResponse, HttpRequest
class Topicos(View):
def post(self, request):
req = request.read().decode(self.request.encoding)
url(r'^api/Topicos', Topicos.Topicos.as_view(), name='Topicos'),
POST /api/Topicos HTTP/1.1
Host: localhost:55020
Connection: keep-alive
Content-Length: 93
Accept: application/json, text/plain, */*
Origin: http://localhost:55020
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
X-CSRFToken: VRiCt3Lz1EIdKGWt3lknpcgdFpD8XbrwSxdPT4P9dd1tbrGYmgE8uHdEIH2dzP5h
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:55020/
Accept-Encoding: gzip, deflate, br
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: csrftoken=VRiCt3Lz1EIdKGWt3lknpcgdFpD8XbrwSxdPT4P9dd1tbrGYmgE8uHdEIH2dzP5h
{"titulo":"Título 1","mensagem":"mensagem","email":"fla...@email.test.br","usuario":"Flávio"}
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e883f24-bea9-4ffe-b183-741ea919fefe%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f6e850f3-89e5-4347-afe4-00b32a2a198e%40googlegroups.com.
I have never used request.read() or tried decoding the request, and I had no idea that request even had an encoding attribute. You stated that you had the exact same code working elsewhere. Is there some difference in the settings file? Some difference in the environment you are using?
When working with JSON, I use the Django Rest Framework to handle the details and just use request.POST and request.GET without worrying about other details. http://www.django-rest-framework.org/
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f6e850f3-89e5-4347-afe4-00b32a2a198e%40googlegroups.com.