Skip to content

Commit 09903ec

Browse files
github-actions[bot]Repo AssistCopilot
authored
[Repo Assist] docs: add HTTP Authentication section to Http.fsx (#1674)
* docs: add HTTP Authentication section to Http.fsx Document Basic Auth, Bearer token, and Windows/NTLM auth patterns using the existing HttpRequestHeaders helpers and customizeHttpRequest. Closes #158 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger CI checks --------- Co-authored-by: Repo Assist <copilot@github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 0eda330 commit 09903ec

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

docs/library/Http.fsx

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,71 @@ match Http.Request(logoUrl).Body with
241241
| Text text -> printfn "Got text content: %s" text
242242
| Binary bytes -> printfn "Got %d bytes of binary content" bytes.Length
243243

244+
(**
245+
## HTTP Authentication
246+
247+
FSharp.Data provides built-in helpers in `cref:T:FSharp.Data.HttpRequestHeaders` for the most common authentication schemes.
248+
249+
### Basic authentication
250+
251+
Use `cref:M:FSharp.Data.HttpRequestHeaders.BasicAuth` to add an `Authorization: Basic …` header.
252+
The helper encodes the credentials as UTF-8 before base64-encoding them:
253+
*)
254+
255+
(*** do-not-eval ***)
256+
257+
Http.RequestString(
258+
"https://api.example.com/data",
259+
headers = [ HttpRequestHeaders.BasicAuth "myUsername" "myPassword" ]
260+
)
261+
262+
(**
263+
### Bearer / token authentication
264+
265+
Use `cref:M:FSharp.Data.HttpRequestHeaders.Authorization` to send any other `Authorization` header value,
266+
such as a Bearer token used by OAuth 2.0 or personal-access-token APIs:
267+
*)
268+
269+
(*** do-not-eval ***)
270+
271+
let token = "<your-access-token>"
272+
273+
Http.RequestString(
274+
"https://api.github.com/user",
275+
headers =
276+
[ HttpRequestHeaders.Authorization(sprintf "Bearer %s" token)
277+
HttpRequestHeaders.UserAgent "MyApp" ]
278+
)
279+
280+
(**
281+
### Windows / NTLM integrated authentication
282+
283+
For services that require Windows Integrated Authentication (NTLM or Negotiate), use the
284+
`customizeHttpRequest` parameter to set the credentials on the underlying `HttpWebRequest`:
285+
*)
286+
287+
(*** do-not-eval ***)
288+
289+
open System.Net
290+
291+
// Use the current Windows user's credentials
292+
Http.RequestString(
293+
"https://intranet.example.com/api/data",
294+
customizeHttpRequest =
295+
fun req ->
296+
req.UseDefaultCredentials <- true
297+
req
298+
)
299+
300+
// Or supply explicit credentials
301+
Http.RequestString(
302+
"https://intranet.example.com/api/data",
303+
customizeHttpRequest =
304+
fun req ->
305+
req.Credentials <- NetworkCredential("username", "password", "DOMAIN")
306+
req
307+
)
308+
244309
(**
245310
## Customizing the HTTP request
246311

0 commit comments

Comments
 (0)