#6737·scrapy

Content-Length header not being calculated automatically

Author: arielpontesCreated Mar 17, 2025Updated Sep 26, 2026

Description

As far as I understand, Scrapy is supposed to calculate and send the Content-Length header automatically. I couldn't find this anywhere in the documentation, but that's what I read elsewhere. When I send a POST request, however, the Content-Length header is never sent.

Is it true that Scrapy is supposed to populate the Content-Length header automatically? If not, how am I supposed to calculate it manually? Shouldn't this be documented?

Steps to Reproduce

Let's consider the following spider:

import scrapy
from scrapy.http import JsonRequest


class TestSpider(scrapy.Spider):
    name = "test_spider"

    def start_requests(self):
        url = "https://dummyjson.com/test"
        payload = {"key1": "value1", "key2": "value2"}
        yield JsonRequest(url=url, data=payload, callback=self.parse)

    def parse(self, response):
        print(response.request.headers)

Expected behavior: The Content-Length header is present in the printed headers and its value corresponds to the size of the request body.

Actual behavior: The Content-Length header is not present at all.

Reproduces how often: 100% of the time

Versions

Scrapy : 2.12.0 lxml : 5.3.0.0 libxml2 : 2.12.9 cssselect : 1.2.0 parsel : 1.9.1 w3lib : 2.2.1 Twisted : 24.3.0 Python : 3.11.9 (main, Nov 25 2024, 15:09:47) [Clang 16.0.0 (clang-1600.0.26.4)] pyOpenSSL : 24.2.1 (OpenSSL 3.3.2 3 Sep 2024) cryptography : 43.0.3 Platform : macOS-15.3.2-arm64-arm-64bit