Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate content length POST

Tags:

http

post

How can I calculate content length for example of:

POST /Upload/ HTTP/1.1
Host: test.lan
User-Agent: Shockwave Flash
Connection: Keep-Alive
Cache-Control: no-cache
Accept: text/*
Content-Length: ?????
Content-Type: multipart/form-data; boundary=----------------------------4d2179e6b3c0

------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Filename"

phpinfo.php
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="ASPSESSID"

6e223eb1c7561e9c599f03cc04e9444b
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Filedata"; filename="phpinfo.php"
Content-Type: application/octet-stream

<? phpinfo(); ?>
------------------------------4d2179e6b3c0
Content-Disposition: form-data; name="Upload"

Submit Query
------------------------------4d2179e6b3c0--
like image 516
Someone1234 Avatar asked Apr 15 '12 11:04

Someone1234


People also ask

How do you calculate content length?

As bytes are represented with two hexadecimal digits, one can divide the number of digits by two to obtain the content length (There are 12 hexadecimal digits in "48656c6c6f21" which equates to six bytes, as indicated in the header "Content-Length: 6" sent from the server).

How is Postman content length calculated?

For Content-Length and Content-Type headers, Postman will automatically calculate values when you send your request, based on the data in the Body tab. However, you can override both values. Once your headers and other request details are set up, you can select Send to run your request.

What is content length in post HTTP?

14.13 Content-Length The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

Is content length required for post?

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.


1 Answers

The Content-Length value should be calculated by totaling all data after the termination of the message headers. In the case of your example, this is everything after this point (with CRLF characters included for readability):

...
Content-Length: ?????\r\n
Content-Type: multipart/form-data; boundary=--------------------4d2179e6b3c0\r\n
\r\n

Everything coming after the first empty line (\r\n) -- including your boundary delimiters -- should be counted in the total length. In practice, this usually means that you'll need to tabulate the Content-Length header value after generating the full message entity body. Once you have the full body of the message you can prepend it with your headers to create the full HTTP message.

According to the HTTP spec you aren't technically required to specify the Content-Length header. From RFC 2616 14.13:

Applications SHOULD use this field to indicate the transfer-length of the message-body, unless this is prohibited by the rules in section 4.4.

However, this is a pretty standard requirement for most servers which will generally send back an error response if the Content-Length is missing or incorrectly specified.

like image 70
rdlowrey Avatar answered Oct 30 '22 14:10

rdlowrey