Archived Forum Post

Index of archived forum posts

Question:

Get attachment filename from CkHttp's Download method.

Jan 08 '15 at 07:46

Background: I need to get the attachment filename provided by the http response while using the CkHttp Download() method. There does not seem to be an integrated method for that specifically and that is just fine since it may be always be provided in the response.

Problem: After downloading the file I can call get_LastResponseHeader() or one of the similar methods, so that I can parse the response header. However I am not able to get the header as a CkHttpResponse object or even initialize a new CkHttpResponse object from the string returned by get_LastResponseHeader().

Request: The reason for me to want to retrieve the response as a CkHttpResponse object is that CkHttpResponse has built-in methods for working with the individual headers in the response. I could manually parse the response string and simply look for the attachment header, but my request would seem like a more elegant solution.

Alternative: In case I have missed something here, please let me know. Maybe I should not even use the Download() method here, but HttpSynchronousRequest() instead.


Accepted Answer

HTTP requests and responses are MIME. You can load the LastResponseHeader string into a CkMime object, and then use CkMime's API to get the contents of the header fields, as well as attributes within structured header fields. It shouldn't matter that the MIME you're loading into the CkMime object is bodyless -- it will still load and parse the header..


Answer

Thanks, it works perfectly with the following code (for future reference):

CkMime mime;
mime.UnlockComponent(...);
mime.LoadMime(http.lastResponseHeader());
const char *fileName = mime.getHeaderFieldAttribute("Content-Disposition", "filename");

I am happy to see that it is case-insensitive :-)