Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP POST: content-length header required?

I'm currently trying to optimize http-based data transfer between several applications. Our current approach, downloading first and then creating the post-request, obviously add extra IO/memory load and latencies, which I'd like to circumvent.

The core question of all:

Is it required to send a "Content-Length" header in HTTP POST requests? IIRC, HTTP 2616 declares that it's optional, but I'm not sure how applications actually behave at this point.

like image 447
Enrico 'nekrad' Weigelt Avatar asked Feb 07 '13 18:02

Enrico 'nekrad' Weigelt


People also ask

Is Content Length header required in POST request?

Description. The Content-Length is optional in an HTTP request. For a GET or DELETE the length must be zero. For POST, if Content-Length is specified and it does not match the length of the message-line, the message is either truncated, or padded with nulls to the specified length.

How do I send Content header Length?

To manually pass the Content-Length header, you need to add the Content-Length: [length] and Content-Type: [mime type] headers to your request, which describe the size and type of data in the body of the POST request.

Which HTTP headers are mandatory?

Common Response HeadersThe first line of the response is mandatory and consists of the protocol ( HTTP/1.1),response code (200)and description (OK). The headers shown are: CONTENT-Type -This is Text/html which is a web page. It also includes the character set which is UTF-8.

Is Content-Type header required?

No, it's not mandatory. Per the HTTP 1.1 specification: Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.


1 Answers

Depends what you mean by optional. If you mean that you can just omit the header anytime you like then no, it is not optional. The HTTP spec has very specific rules when to use that header. There are different ways of sending the data if you don't know the length. Chunked encoding for example.

4.4 Message Length

like image 130
jdb Avatar answered Sep 23 '22 02:09

jdb