Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify headers for a web request

Trying to set headers for a http call but running into issues. Need guidance for both Authorization and a custom 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

The error I'm getting is

error FS3217: This expression is not a function and cannot be applied. Did you intend to access the indexervia expr.[index] instead?

like image 810
Developer11 Avatar asked Dec 13 '25 18:12

Developer11


1 Answers

The error message that you are getting actually tells you what the problem is. In F#, the syntax for using an indexer is obj.[idx] - you need to have a . between the object and the square bracket. The correct syntax in your specific case would be:

request.Headers.["x-api-key"] <- "api-key"
request.Headers.["Authorization"] <- sprintf "%s %s" token.token_type token.access_token
like image 170
Tomas Petricek Avatar answered Dec 16 '25 20:12

Tomas Petricek