How to consume HttpClient from F#?

后端 未结 5 1781
攒了一身酷
攒了一身酷 2021-02-20 04:13

I\'m new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#:



        
5条回答
  •  感情败类
    2021-02-20 05:13

    Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to use the let! syntax):

    let getAsync (url:string) = 
        async {
            let httpClient = new System.Net.Http.HttpClient()
            let! response = httpClient.GetAsync(url) |> Async.AwaitTask
            response.EnsureSuccessStatusCode () |> ignore
            let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask
            return content
        }
    

提交回复
热议问题