Skip to content

Behaviour post/json request content vs. method read()  #2914

Open
@Pajinek

Description

@Pajinek

Long story short

I have created middleware for forwarding requests and I need to read content of them. I figure out behaviour that it is rather strange for me.

I send request (post/get) and text/read return body of message (post/json data is included). But content (StreamReader instance,) returns empty string.

And documentation says:

content
A StreamReader instance, input stream for reading request’s BODY.
Read-only property.

Expected behaviour

I suppose that content is body of message (https://docs.aiohttp.org/en/stable/web_reference.html#aiohttp.web.BaseRequest.content)

Actual behaviour

Method request.content.read() return empty string as result.

Steps to reproduce

send request:

  1. await test_app.post("/get_fce", data={"hello": "kitty"})
 >>> print(await request.read())
b'hello=kitty'
 >>>  print(await request.text())
hello=kitty
 >>>  print(await request.content.read())
b''
  1. await test_app.post("/get_fce", json={"hello": "kitty"})
 >>> print(await request.read())
b'{"hello": "kitty"}'
 >>> print(await request.text())
{"hello": "kitty"}
 >>> print(await request.content.read())
b''

Your environment

aiohttp==3.1.0
aioredis==1.1.0
async-timeout==2.0.1 # via aiohttp
attrs==17.4.0 # via aiohttp
chardet==3.0.4 # via aiohttp
idna-ssl==1.0.1 # via aiohttp
multidict==4.1.0 # via aiohttp, yarl
ujson==1.35
yarl==1.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    HacktoberfestWe think it's good for https://hacktoberfest.digitalocean.com/documentationImprovements or additions to documentationenhancementgood first issueGood for newcomers

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions