aio-libs / aio-libs/aiohttp

Accessing Response.text can fail with an AttributeError

Đang mở
#2,928 10 bình luận 0 reaction 1 người được giao Được @myusko nhận Xem trên GitHub
bug good first issue Hacktoberfest server
Ngôn ngữ chính
Python
Star
16.5k
Fork
2.4k
Merge trung bình
17 giờ 22 phút
Pull request đã merge (30 ngày)
212

Mô tả

## Long story short
Handling of payloads in the server response is not consistent and can lead to `AttributeError`s since code depends on methods that may not exist. I am willing to try and create a PR that fixes this issue but I'm unsure of what the intended behavior should be.

## Expected behaviour
We handle payload setting consistently and don't fail when `text` is called after setting a string payload using the `body` property setter.

## Actual behaviour
The setter for `Response.body` has support for automatic type detection via the `PayloadRegistry`. This means that in the above example, the body will be encoded as a `StringPayload`. When trying to access this via the `text` property we fail because `StringPayload` does not have a `decode` method.

When you set `text` directly any string is decoded to `bytes` and set to `_body` directly without passing through the `PayloadRegistry`.

## Steps to reproduce
```python
>>> from aiohttp.web_response import Response
>>> r = Response()
>>> r.body = 'asdf'
>>> r.text
Traceback (most recent call last):
File "", line 1, in
File "/private/tmp/.ve/lib/python3.6/site-packages/aiohttp/web_response.py", line 539, in text
return self._body.decode(self.charset or 'utf-8')
AttributeError: 'StringPayload' object has no attribute 'decode'
```

## Your environment
Reproduced on aiohttp 2.3.10 and 3.1.2

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.