Content-Length header in request

176 views
Skip to first unread message

Alexander Bulaev

unread,
Dec 14, 2012, 1:11:13 PM12/14/12
to spray...@googlegroups.com
Hi!

I am working with spray-client and I have a problem with attaching Content-Length header to the request.
I'm sayling something like this:
Put("/v1/CLODO_3a1961c91cd596e337943d792e5f70e5") ~> (
  addHeader(`Content-Length`(0)) ~>
  sendReceive(httpConduit)
)

But on the wire the request misses this header:
PUT /v1/CLODO_3a1961c91cd596e337943d792e5f70e5 HTTP/1.1
User-Agent: spray-can/1.1-M6

Am I missing something?

Alexander

Mathias

unread,
Dec 17, 2012, 9:55:37 AM12/17/12
to spray...@googlegroups.com
Alex,

you cannot add a Content-Length header yourself, the spray-can layer takes care of this.
However, your question raises an issue that apparently has been asked by many implementors of HTTP client functionality before:
When exactly are `Content-Length: 0` headers required?

Unfortunately RFC2616 is a bit cloudy in that regard so I've done a bit more research into this. The best discussion I could find is this one:
http://lists.w3.org/Archives/Public/ietf-http-wg/2006AprJun/0096.html

Up to now we only rendered a `Content-Length: 0` header if a request entity was actually present.
However, the best approach appears to be to include a `Content-Length: 0` in requests with a method for which the meaning of a request entity is properly defined.
This means that GET requests do not receive a `Content-Length: 0` header because the meaning of GET entities is not defined (even though the spec does not explicitly forbid them).
PUT request on the contrary should receive a `Content-Length: 0` header.

I have just added a patch implementing this, it'll be part of the next nightly.

Thanks for bringing this up!

Cheers,
Mathias

---
mat...@spray.io
http://spray.io
> --
>
>

Alexander Bulaev

unread,
Dec 17, 2012, 11:55:13 AM12/17/12
to spray...@googlegroups.com
Mathias,

I am implementing a client for OpenStack Swift blob storage. Though it is not reflected in the official docs, current version of Swift
installed on my cloud provider's machines does not work when PUT requests are made with empty body and without "Content-Length: 0" header.
I don't know if this is a Swift's bug, but this does not matter, because obviously I have no control of provider's software. So the only possible
workaround for me is to attach this header.

I agree with you that PUT requests should have this header. What about POST and PATCH requests?

Thanks for your investigation!

Mathias

unread,
Dec 18, 2012, 5:57:46 AM12/18/12
to spray...@googlegroups.com
Alex,

> I agree with you that PUT requests should have this header. What about POST and PATCH requests?

Yes, as of todays nightly build OPTIONS, PUT, POST and PATCH requests carry a `Content-Length: 0` header if the entity is empty.

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

> --
>
>

Alexander Bulaev

unread,
Dec 18, 2012, 1:49:30 PM12/18/12
to spray...@googlegroups.com
Mathias,
thanks! I will download the latest nightly build and test it.
Reply all
Reply to author
Forward
0 new messages