http - F# - Http 请求响应主体到字符串以与 json 类型提供程序一起使用

标签 http f# type-providers f#-data

我从网络 api 得到响应,它只返回与请求 url 中的 id 对应的数据的 F# 记录。

我的问题是如何获取 response.Body 的文本表示,以便我可以将其插入下面注释掉的行并将其与 json 类型提供程序一起使用。或者有没有我想念的不同方法,

let response = Http.Request("http://localhost:50442/api/getrates/" + input)

        let sample = Json.Sample.Parse(""" {"CurrencyId":1,"CurrencyCode":"AUD","Value":0.98542} """)
        //let sample = Json.Sample.Parse("\"\"\" " + response.Body.ToString() + " \"\"\"")

        let data = sample.Value

        return "Rate for: " + input + " = " + data.ToString()

最佳答案

您需要一些示例 JSON 供类型提供程序在编译时检查(来自本地文件、文字字符串或来自远程 URL),用于检测架构和生成类型。

然后在运行时动态加载一些与相同模式匹配的新数据集,并且您具有类型化访问权限。

// provide some URL, file path, or raw JSON sample
// that can be used at compile time to generate types
type Json = JsonProvider<"sample.json">


// at runtime, load up new sample matching that schema
let response = Http.Request("<some url that serves JSON at runtime>")
let samples = Json.Parse(response.Body.ToString())

// or do it this way
let samples = Json.Load("<some url that serves JSON at runtime>")

// magic!
sample.[0].EyeColor

关于http - F# - Http 请求响应主体到字符串以与 json 类型提供程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29283350/

相关文章:

php - 从某个页面中删除 https

ios - 在 iOS 中使用 JSON 发送 HTTP POST 请求

f# - 使用 OperatorPrecedenceParser 解析不区分大小写的运算符

f# - 您可以在创建类型提供程序之前运行代码吗? (F#)

http - Firefox 中奇怪的 HTTP/2 HPACK 编码

VBA Microsoft.XMLHTTP setRequestHeader 不发送 cookie

visual-studio - 如何在#light 模式下在 Visual Studio 2008 中缩进 F# 代码

f# - 如何在 View 模型上使用 DelegateCommand?

f# - 记录 F# 中的变化

f# - FSharp : Using CSV Type Provider Async