Calculate content length POST - http

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--

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.

Related

Upload image from URL through Drive API

I'm using Postman to learn how to use the Google Drive API.
I'm trying to upload an API to an image that is in a URL. I'm using the multipart upload described in this documentation link, but the example is not clear to me, I can upload a file with the metadata I'm passing, but the image of the URL I've placed is not being sent.
Please help me understand what I'm doing wrong. Below is the code for my request.
POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Content-length: *
Authorization: [myToken]
Content-Type: multipart/related; boundary=test
Cache-Control: no-cache
Postman-Token: [postmanToken]
--test
Content-Type: application/json; charset=UTF-8
{
"name": "imageTest",
"parents": ["1Ij0ZR8yrubkHQaT6tSQNgK6AhW4gbP49"]
}
--test
Content-Type: image/*
https://download.shutterstock.com/gatekeeper/W3siZSI6MTUzNjcwMzQ0NSwiayI6InBob3RvLzEwMDcwNjYwMjMvaHVnZS5qcGciLCJtIjoxLCJkIjoic2h1dHRlcnN0b2NrLW1lZGlhIn0sInR3UkJ0bDZNYUJNUXJ2azZlaE9MbTZlT2VBbyJd/shutterstock_1007066023.jpg
--test--
I doubt that you will be able to use Postman to upload a file. Its going to have to read the file first and then send the file data in chunks.
All of the examples google has for this is using a programming language there is no samples of doing it directly with HTTP calls file upload
If it did work you would be looking at something like this. You are still going to need some language to read the file contents and add it to this.
POST / HTTP/1.1
[[ Less interesting headers ... ]]
Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
Content-Length: 834
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text1"
text default
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="text2"
aωb
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file1"; filename="a.txt"
Content-Type: text/plain
Content of a.txt.
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file2"; filename="a.html"
Content-Type: text/html
<!DOCTYPE html><title>Content of a.html.</title>
-----------------------------735323031399963166993862150
Content-Disposition: form-data; name="file3"; filename="binary"
Content-Type: application/octet-stream
aωb
-----------------------------735323031399963166993862150--

Uploading file gets Bad Request from the server

I am trying to send a file using HTTP from a C++ application (no HTML-boxes). The server keeps answering Code 400/ Bad Request.
To keep it simple, I have changed manually the content of the file to a simple string (later on, I will need to upload real binary files).
the POST request is the following:
POST /post.php HTTP/1.0
Host: posttestserver.com
Accept: */*
Content-Type: multipart/form-data; boundary=BOUNDARY
--BOUNDARY
Content-Disposition: form-data; name="userfile"; filename="example.txt"
Content-Type:text/plain
123ABC
--BOUNDARY--
Connection: close
Any idea what is going on?

HTTP Request\Response Header Grammar

In the header of an HTTP request or response will the header keys be constant in terms of capitalization, between servers.
I ask so I can expect in my code: (Using Fake Function names)
Safe Precise Python Code
for hdr in header.keys():
if 'content-length' == hdr.lower():
recv_more_data( header[hdr] ) # header[hdr] == Content-Length (5388) bytes
break # Exit for loop when if statement is met.
Code I Would Like To Use
recv_more_data (header['Content-Length'])
# I know to expect 'Content-Length' not 'content-Length' or some other variation
Meaning will a server ever return a header with the keys like so.
Standard Request
GET / HTTP/1.1
Host: www.example-host.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0
Accept: */*
Accept-Language: en-US
Accept-Encoding: gzip
Connection: closed
Content-Length: 0
A Bad But Possible Response?
HTTP/1.1 200 OK
Server: nginx/1.0.15
date: Thu, 23 Oct 2014 00:25:37 GMT
content-Type: text/html; charset=iso-8859-1
transfer-encoding: chunked
Connection: close
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
Clarification will help my code neatness.
HTTP header names are case-insensitive, per the HTTP specification.
RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1
Section 4.2 - Message Headers
HTTP header fields, which include general-header (section 4.5),
request-header (section 5.3), response-header (section 6.2), and
entity-header (section 7.1) fields, follow the same generic format as
that given in Section 3.1 of RFC 822 [9]. Each header field consists
of a name followed by a colon (":") and the field value. Field names
are case-insensitive. The field value MAY be preceded by any amount
of LWS, though a single SP is preferred. Header fields can be
extended over multiple lines by preceding each extra line with at
least one SP or HT. Applications ought to follow "common form", where
one is known or indicated, when generating HTTP constructs, since
there might exist some implementations that fail to accept anything
beyond the common forms.
RFC 7230 - Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
Section 3.2 - Header Fields:
Each header field consists of a case-insensitive field name followed
by a colon (":"), optional leading whitespace, the field value, and
optional trailing whitespace.
HTTP header names are case insensitive.
It looks like you're using python. Check out the requests library. It'll make your life much easier: http://docs.python-requests.org/en/latest/
Bear in mind that, even though most major servers will have consistent capitalization, any Joe PHP Developer can set the response headers manually in their code - and there is no way to police what that guy uses as a capitalization standard.

Create multipart http post

I have a visual program with which I am trying to create a http multipart post.
I have two context variables that contain, respectively, a string and a file.
The visual program consists of header and body,detached. In the header i insert
Content-Type multipart/form-data,but how can insert the variabile in the body and how can i indicate the different part the body and her name,how can insert in the header?
Your request could be formed as follows:
POST /your_page.html HTTP/1.1
Host: your.host.com
Content-Type: multipart/form-data; boundary=My_Boundary_1234567890
Content-Length: [length of request in bytes]
--My_Boundary_1234567890
Content-Disposition: form-data; name="MyVariableName"
my_valiable_value
--My_Boundary_1234567890
Content-Disposition: form-data; name="MyFile"; filename="picture.jpg"
Content-Type: image/jpeg
binary contents of the file
--My_Boundary_1234567890--
Just do not forget about new line after the last boundary.

Issues with valid multipart/mixed and Google Drive

I am attempting to upload a file to Google Drive using the "upload" URL with a type of "multipart". I'm trying to do this without a library and using basic HTTP with a multipart POST. With a body like the following, I am constantly getting the error "Invalid multipart request with 0 mime parts."
The HTTP message looks valid to me. Is there something obvious that I'm missing or doing wrong?
Is there a protocol tester that can verify if my POST body is valid or not?
POST /upload/drive/v2/files?uploadType=multipart HTTP/1.1
Authentiction: Bearer {valid auth_token}
Content-Type: multipart/mixed; boundary="--314159265358979323846"
host: localhost:3004
content-length: 254
Connection: keep-alive
--314159265358979323846
Content-Type: application/json
{"title":"Now","mimeType":"text/plain"}
--314159265358979323846
Content-Type: text/plain
Content-Transfer-Encoding: 8bit
Mon Jun 17 2013 20:59:02 GMT-0400 (EDT)
--314159265358979323846--
(The segments look like they have double newlines. I think this is an artifact of the pasting, they are CRLF pairs in the code and appear as a newline when testing, but I guess this could theoretically be the problem, but I'd like proof.)
boundary attribute on the Content-Type header should not include double dashes. Use the following as your Content-Type:
Content-Type: multipart/mixed; boundary="314159265358979323846"

Resources