Archived Forum Post

Index of archived forum posts

Question:

File upload WebDav

Mar 27 '13 at 11:26

Hi there, I want to upload a file to a WebDav server using the C++ libraries. Below is the code I'm using: CkHttpRequest req; req.put_HttpVerb("PUT"); req.put_ContentType("multipart/form-data"); CStringA strPath; strPath.Format("/%s", uploadName); req.put_Path(strPath); req.AddFileForUpload(NULL, strFile); const char *pHead = req.generateRequestText();

which gives in pHead:

PUT /public/report.txt HTTP/1.1 Host: HOSTNAME Content-Type: multipart/form-data; boundary=------------020808030003050400040405 Content-Length: 4319

--------------020808030003050400040405 Content-Disposition: form-data; name=""; filename="report.txt" Content-Type: text/plain ...

However the WebDav server does not understand "multipart/form-data" and stores

--------------080806070104020804050800 Content-Disposition: form-data; name=""; filename="report.txt" Content-Type: text/plain

at the begin of the files and the boundary markers too... The header is fine, just sending the file data without the boundary stuff would do. Is there any other way to accomplish the task using CkHttp and CkHttpRequest?

Thanks, Dave


Answer

You should become familiar with what the actual HTTP request it is that you're trying to send would look like. For example, if you wanted this:

PUT /pub2/folder1/folder2/item1.txt HTTP/1.1
Content-Type: text/plain
Content-Length: 4

abcd

Then you would call the http.PutText method.


Answer

Well, eeemh. 
Yes, I know there are several methods to upload but I want to upload a LARGE file which 
I cannot load in a buffer first.
Lets say:
PUT /pub2/folder1/folder2/item1.txt HTTP/1.1
Content-Type: text/plain
Content-Length: 9891789794

... the data ...
How would I do that with chilkat?

Answer

Build your HTTP request using the CkHttpRequest object.

With the CkHttpRequest object...
Set the ContentType property = "text/plain".
Set the HttpVerb property = "PUT".
Set the Path property to whatever it should be.
Set the body by calling StreamBodyFromFile (see the reference documentation)

Send the request via the http.SynchronousRequest method.


Answer

Hi, in fact I wasn't aware of StreamBodyFromFile. While the proposed solution looks good, 
I don't get it working. What I did:

CkHttpRequest req;
req.put_ContentType("text/plain");
req.put_HttpVerb("PUT");
req.put_Path(strPath);
bool br = req.StreamBodyFromFile(strFile);
CkHttpResponse *resp =  m_http.SynchronousRequest(m_strHost, 80, false, req);

Using PUT as verb there is no Content-Length at all in the header.
Using POST as verb the Content-Length is there, but is always 0.
The Header is sent in both cases and the file contents is streamed too, but without a valid 
Content-Length the server is not happy. BTW, I can't set Content-Length for myself.

Any ideas?

Regards, Dave