尝试为 http 调用设置 header 但遇到问题。需要有关 Authorization
和自定义 header x-api-key
的指导。
let url = "http://example.com"
let token = requestToken()
let request = WebRequest.Create(url) :?> HttpWebRequest
request.Method <- "GET"
request.Accept <- "application/json;charset=UTF-8"
request.Headers.Authorization <- sprintf "%s %s" token.token_type token.access_token
request.Headers["x-api-key"] <- "api-key" // custom headers
// or this
request.Headers["Authorization"] <- sprintf "%s %s" token.token_type token.access_token
我得到的错误是
error FS3217: This expression is not a function and cannot be applied. Did you intend to access the indexervia expr.[index] instead?
最佳答案
您收到的错误消息实际上告诉您问题出在哪里。在 F# 中,使用索引器的语法是 obj.[idx]
- 您需要在对象和方括号之间添加一个 .
。在您的特定情况下正确的语法是:
request.Headers.["x-api-key"] <- "api-key"
request.Headers.["Authorization"] <- sprintf "%s %s" token.token_type token.access_token
关于f# - 如何为 Web 请求指定 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49290064/