Set bodyproducer with empty content for POST

This commit is contained in:
Paul Tremberth 2017-03-23 11:52:01 +01:00
parent 776129a951
commit 99e3c0d653
1 changed files with 1 additions and 13 deletions

View File

@ -280,19 +280,7 @@ class ScrapyAgent(object):
if request.body:
bodyproducer = _RequestBodyProducer(request.body)
else:
bodyproducer = None
# Setting Content-Length: 0 even for POST requests is not a
# MUST per HTTP RFCs, but it's common behavior, and some
# servers require this, otherwise returning HTTP 411 Length required
#
# RFC 7230#section-3.3.2:
# "a Content-Length header field is normally sent in a POST
# request even when the value is 0 (indicating an empty payload body)."
#
# Twisted Agent will not add "Content-Length: 0" by itself
if method == b'POST':
headers.addRawHeader(b'Content-Length', b'0')
bodyproducer = _RequestBodyProducer(b'') if method == b'POST' else None
start_time = time()
d = agent.request(
method, to_bytes(url, encoding='ascii'), headers, bodyproducer)