TL;DR: Given an arbitrary filename as a Go string
value, what's the best way to create a Content-Disposition
header field that specifies that filename?
I'm writing a Go net/http handler, and I want to set the Content-Disposition
header field to specify a filename that the browser should use when saving the file. According to MDN, the syntax is:
Content-Disposition: attachment; filename="filename.jpg"
and "filename.jpg"
in an HTTP "quoted-string". However, I don't see any mention of "quote" in the net/http docs. Only mentions of HTML and URL escaping.
Is quoted-string the same as or at least compatible with URL escaping? Can I just use url.QueryEscape or url.PathEscape for this? If so, which one should I use, or are they both safe for this purpose? HTTP quoted-string looks similar to URL escaping, but I can't immediately find anything saying whether they're compatible, or if there are edge cases to worry about.
Alternatively, is there a higher-level package I should be using instead that can handle the details of constructing HTTP header field values that contain parameters like this?