3

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 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the late response, this was just what I needed, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.