request.read() is empty in POST

431 views
Skip to first unread message

Flávio Cardoso

unread,
Dec 30, 2016, 4:52:49 PM12/30/16
to Django users
Hello! I'm getting crazy, PLEASE someone, give me some light!!!

I'm using Django 1.10.4, Python 3.5 on Windows using Visual Studio Community 2015.

I have some class-based views to response some json.

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)

Have the following setting on urls.py:

url(r'^api/Topicos', Topicos.Topicos.as_view(), name='Topicos'),

The request.read() is empty, the request is: 

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"}


I really don't know what else I can do. I have the same code (except na class name) running correctly to another posts.














Flávio Cardoso

unread,
Jan 3, 2017, 10:11:30 PM1/3/17
to Django users
Wow, that's sad.... :(

I'll port it to C#

Vijay Khemlani

unread,
Jan 3, 2017, 11:26:54 PM1/3/17
to django...@googlegroups.com
Yeah, because hitting a roadblock only happens in Django

Why are you trying to read the request directly? django parses it in
request.body, request.GET, request.POST, request.FILES, etc

https://docs.djangoproject.com/en/1.10/ref/request-response/
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@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/1da72b67-1eb4-4dde-9e13-b3faa8d7c0c0%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Flávio Cardoso

unread,
Jan 4, 2017, 8:22:33 AM1/4/17
to Django users
I work with ASP.Net since version 1.1. I've never been with a stuck project.

I'm trying to directly read the request because it is application/json, so Django doesn't delivery the data via request.GET nor .POST.

And I got an exception on request.body: raise RawPostDataException("You cannot access body after reading from request's data stream")


So, nevermind, you know, time is money. I was trying to learn new stuff, but I'm having many problems. Thank you.

Avraham Serour

unread,
Jan 4, 2017, 10:13:43 AM1/4/17
to django-users
That's an incredibly childish response and I hope future people trying to learn python or django don't give up upon a roadblock, any future people reading this don't ever feel that you are helpless, the community has many resources to help including this mailing list.

Sometimes it is difficult to express yourself in a foreign language, I always suggest to join local mailing list and contact people in your own mother tongue.

Sometimes it is difficult to formulate a clear question in writing, specially in something one is not familiar with, I suggest looking for local meetups and talk personally about your bug.
There are also some mentoring programs in some local communities, be sure to ask on local mailing lists for those

Time is money, and it is obvious that it takes time to learn anything, take into consideration that everyone here is donating their own time.

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.

Flávio Cardoso

unread,
Jan 4, 2017, 3:06:09 PM1/4/17
to Django users
Childish? Ok, so I'm stuck, my project is stopped, nobody, no one, even YOU, can give me a hand. I spent days researching and debugging trying to solve the problem.

I've been waiting for days for anything and nothing, nobody, including YOU told nothing to help me.

If the platform - AND THE COMUNITY - can't give help nor results, I'll jump to another, simply that. The documentation is also poor.

And you're donating your own time to what? Complain? Did you give me some help? No!

So, if you're feeling bad with it, sorry, but I have to give value to my clients, not lose my time with children complaining because I gave up with a product that can't attend me.

Wont you point a solution instead of being a crying baby?

ludovic coues

unread,
Jan 4, 2017, 3:19:09 PM1/4/17
to django...@googlegroups.com
Have you tried reading request.body ?
You can use `print(repr(request.body))` to check the value of
request.body. It should give you more information. I hope that help
you
> https://groups.google.com/d/msgid/django-users/f6e850f3-89e5-4347-afe4-00b32a2a198e%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Avraham Serour

unread,
Jan 4, 2017, 3:19:48 PM1/4/17
to django-users
aand you are angry.

So we can start yet another flame war, if that's the case I'll reply to each of your points.

If you still want help you may reply to the first reply of this thread, which was trying to help but was ignored. I'm also willing to call you and help for at least 20min, just send me a landline number and if it is from one of the 50 countries included in my plan I will make the effort, it seems you are from Brasil, if that's the case honestly I'm afraid you might be angry and impatient, but I'm willing to take the risk.

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.

Matthew Pava

unread,
Jan 4, 2017, 3:23:09 PM1/4/17
to django...@googlegroups.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/

Michal Petrucha

unread,
Jan 4, 2017, 3:55:19 PM1/4/17
to Django users
Hi Flávio,

The behavior of request.read() is somewhat icky, and there are some
caveats when trying to use it. Let me give you a lengthy answer
starting from the basics down to the nasty corner cases that you're
likely encountering.

Basically, there are several interfaces that make it possible to
access the request body:

1. request.POST, and request.FILES – these two are the most high-level
easy-to-use dict-like objects that automatically attempt to parse
the request body, but they only support multipart/form-data and
application/x-www-form-urlencoded requests, otherwise they will be
empty.

2. request.body loads the entire request body into memory on first
access, and stores it as a bytestring in an attribute. This is good
for relatively short requests that fit into memory, if they aren't
URL-encoded or multipart (most commonly JSON or XML).

3. Finally, there's request.read(), request.readline(), iteration, and
some other file-like methods intended for requests that are too
large to fit into memory, which means you need to process them in a
streaming fashion. This means that once you start using this
interface, it is no longer possible to access the part of the
request body that has already been consumed.

Now, an obvious, but not very helpful answer to your question would be
that the request body has already been consumed through request.read
or another file-like access before your call, which means any
subsequent calls to read() or attempts to iterate will return an empty
result.

How could this happen? There are many possibilities, but the most
likely ones are that it's a middleware triggering a read, that your
view is decorated by a decorator that triggers a read, or that one of
the superclasses is responsible. I'd recommend that you consider these
three possibilities first, and look through all applicable
middlewares, decorators and superclasses.

Now, what do you have to look for? Any iteration of the request, calls
to read, readline, readlines, or xreadlines. However, that's not all,
because even just accessing request.POST or request.FILES might under
certain circumstances also have the same effect, although that should
only apply to multipart requests.

It's hard to give you any more specific advice, because the issue is
not in the piece of code that you have posted (which is also probably
the reason why you didn't get any responses to the original question).

However, when dealing with JSON requests, it's usually a better idea
to just use request.body, rather than request.read(), specifically to
avoid this kind of problems. If trying to read request.body is raising
a RawPostDataException on you, complaining that you cannot access body
after reading from request's data stream, that means you need to look
at any code that's executed before that line and track down what is
performing that read earlier.

Good luck...

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages