2
votes

I've been looking at the FSharp.Data package, which looks very useful, and have seen how to do basic requests...

let html = Http.RequestString("http://example.com")

I would like to use this to access password-protected pages, but can't see how to do it. Looking at the HttpRequestHeaders page, it looks like I should be able to pass in some headers to include the information, but I'm not sure how to do it.

That page links to a code sample that includes the following function...

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

...which looks like it's creating the info necessary, but I don't know what to do with it next.

Anyone able to help? Thanks

1

1 Answers

2
votes

BasicAuth creates value of the Authorization HTTP header.

Something like that (adapted from http://fsharp.github.io/FSharp.Data/library/Http.html):

Http.RequestString
  ( "http://example.com/...", httpMethod = "GET",
    headers = [ "Authorization", (BasicAuth "username" "password") ])

There a many resources on the Authorization header, this looks good: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization

This one has a nice diagram: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication