【问题标题】:F# using FSharp.Data not able to get basicAuth workingF# 使用 FSharp.Data 无法获得 basicAuth 工作
【发布时间】:2020-06-16 14:13:43
【问题描述】:

我是 F# 新手,正在尝试使用 FSharp.Data 自动化 API,但我无法让基本身份验证正常工作。我知道有一些我无法破解的语法问题。请建议如何在代码的“(BasicAuth“username_api”“pwd_api”)部分消除此错误:此表达式应具有“string”类型,但此处具有“string * string”类型。

另外,我附上了一张图片供快速参考。

这是我的测试:

[<Test>]
let getBasicAuthUsingData () =
        let Authorization (credentials:string) = "Authorization", credentials

        let BasicAuth (username:string) (password:string) =
            let base64Encode (s:string) =
                let bytes = Encoding.UTF8.GetBytes(s)
                Convert.ToBase64String(bytes)
            sprintf "%s:%s" username password |> base64Encode |> sprintf "Basic %s" |>  Authorization

        let apiURL = "http://sampleapi"


        let response = Http.RequestString( apiURL, httpMethod = "GET", headers = [ "Authorization", (BasicAuth "username_api" "pwd_api")])  
        printfn "Response of get is %s: " response

【问题讨论】:

    标签: f# f#-data


    【解决方案1】:

    您添加了两次标题名称:第一次是在Authorization 函数中,然后出于某种原因在标题列表中。

    因此,标题列表的元素最终成为 ("Authorization", ("Authorization", "user:pass")) 而不仅仅是 ("Authorization", "user:pass")

    Just get rid 是"Authorization" 字符串之一:

    let response = Http.RequestString( apiURL, httpMethod = "GET", headers = [BasicAuth "username_api" "pwd_api"]) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2016-04-01
      相关资源
      最近更新 更多