Skip to content

Instantly share code, notes, and snippets.

@fairjm
Created October 23, 2016 08:08
Show Gist options
  • Save fairjm/8509cb19a4a729d96b51f1f57c995013 to your computer and use it in GitHub Desktop.
Save fairjm/8509cb19a4a729d96b51f1f57c995013 to your computer and use it in GitHub Desktop.
F# get attachment filename by httpclient
open System.Net.Http
open System.Net.Mime
open System.Text
///get fileName by ContentDisposition or random file name if null
let getFileName (msg: HttpResponseMessage) =
let value = Option.ofObj msg.Content.Headers.ContentDisposition
let result =
value
|> Option.map (fun e -> e.FileName)
|> Option.map (fun e -> Encoding.UTF8.GetString(Encoding.GetEncoding("ISO-8859-1").GetBytes(e)))
match result with
| Some(v) -> v
| None -> System.IO.Path.GetRandomFileName()
let fileName = using(new HttpClient()) (fun e ->
let msg = e.GetAsync("https://sukebei.nyaa.se/?page=download&tid=2154757").Result
Utils.getFileName(msg)
)
printfn "%A" fileName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment