使用基本认证发送 HTTP 请求

Elm:
使用基本认证发送 HTTP 请求

How to:

如何操作:

import Http
import Base64

-- Define your endpoint and credentials
endpoint : String
endpoint = "https://your.api/endpoint"

username : String
username = "yourUsername"

password : String
password = "yourPassword"

-- Create the Basic Auth header
basicAuthHeader : Http.Header
basicAuthHeader =
    let
        encodedCredentials =
            Base64.encode (username ++ ":" ++ password)
    in
    Http.header "Authorization" ("Basic " ++ encodedCredentials)

-- Make the HTTP request with Basic Authentication
makeRequest : Http.Request String
makeRequest =
    Http.request
        { method = "GET"
        , headers = [ basicAuthHeader ]
        , url = endpoint
        , body = Http.emptyBody
        , expect = Http.expectString (Result.withDefault "Request failed")
        , timeout = Nothing
        , tracker = Nothing
        }

Deep Dive:

深入了解: 在历史上,基本认证是HTTP协议早期用于验证用户身份的简单方式。如今,虽然存在更安全的替代方案(如OAuth2和JWT),但基本认证因其简单性,在某些情况下仍被使用,特别是在内部系统或少量用户的系统中。实现时,重要的是对凭证进行编码,并在HTTP请求的头部发送。Elm中,利用 Http 包和 Base64 编码来完成这个过程。

See Also:

参见: