Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse the Content-Disposition header to retrieve the filename property?

Tags:

http

go

Using go, how can I parse the Content-Disposition header retrieved from an http HEAD request to obtain the filename of the file?

Additionally, how do I retrieve the header itself from the http HEAD response? Is something like this correct?

resp, err := http.Head("http://example.com/")
//handle error
contentDisposition := resp.Header.Get("Content-Disposition")

The mime/multipart package specifies a method on the Part type that returns the filename (called FileName), but it's not clear to me how I should construct a Part, or from what.

like image 315
Suchipi Avatar asked Mar 03 '15 22:03

Suchipi


People also ask

How do you use content disposition headers?

In a regular HTTP response, the Content-Disposition response header is a header indicating if the content is expected to be displayed inline in the browser, that is, as a Web page or as part of a Web page, or as an attachment, that is downloaded and saved locally.

What is content disposition attachment filename?

Content-Disposition is an optional header and allows the sender to indicate a default archival disposition; a filename. The optional "filename" parameter provides for this. This header field definition is based almost verbatim on Experimental RFC 1806 by R. Troost and S.

What does Err_response_headers_multiple_content_disposition mean?

The response from the server contained duplicate headers. This problem is generally the result of a misconfigured website or proxy. Only the website or proxy administrator can fix this issue. Error 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION): Multiple distinct Content-Disposition headers received.

What is Contentdispositionheadervalue?

A suggestion for how to construct filenames for storing message payloads to be used if the entities are detached and stored in a separate files. ModificationDate. The date at which the file was last modified. Name.


1 Answers

You can parse the Content-Disposition header using the mime.ParseMediaType function.

disposition, params, err := mime.ParseMediaType(`attachment;filename="foo.png"`)
filename := params["filename"] // set to "foo.png"

This will also work for Unicode file names in the header (e.g. Content-Disposition: attachment;filename*="UTF-8''fo%c3%b6.png").

You can experiment with this here: http://play.golang.org/p/AjWbJB8vUk

like image 57
James Henstridge Avatar answered Oct 14 '22 14:10

James Henstridge