Prepare Content-Length header based on request method and body
(self, body: _t.BodyType)
| 650 | self.body = body # type: ignore[assignment] # body transforms from DataType to BodyType |
| 651 | |
| 652 | def prepare_content_length(self, body: _t.BodyType) -> None: |
| 653 | """Prepare Content-Length header based on request method and body""" |
| 654 | if body is not None: |
| 655 | length = super_len(body) |
| 656 | if length: |
| 657 | # If length exists, set it. Otherwise, we fallback |
| 658 | # to Transfer-Encoding: chunked. |
| 659 | self.headers["Content-Length"] = builtin_str(length) |
| 660 | elif ( |
| 661 | self.method not in ("GET", "HEAD") |
| 662 | and self.headers.get("Content-Length") is None |
| 663 | ): |
| 664 | # Set Content-Length to 0 for methods that can have a body |
| 665 | # but don't provide one. (i.e. not GET or HEAD) |
| 666 | self.headers["Content-Length"] = "0" |
| 667 | |
| 668 | def prepare_auth( |
| 669 | self, |
no test coverage detected