in lib/http.go [746:787]
func (l httpLib) basicAuthentication(args ...ref.Val) ref.Val {
if len(args) != 3 {
return types.NewErr("no such overload for request")
}
request, ok := args[0].(traits.Mapper)
if !ok {
return types.ValOrErr(request, "no such overload for do_request")
}
username, ok := args[1].(types.String)
if !ok {
return types.ValOrErr(username, "no such overload for request")
}
password, ok := args[2].(types.String)
if !ok {
return types.ValOrErr(password, "no such overload for request")
}
reqm, err := request.ConvertToNative(reflectMapStringAnyType)
if err != nil {
return types.NewErr("%s", err)
}
// Rather than round-tripping though an http.Request, just
// add the Authorization header into the map directly.
// This reduces work required in the general case, and greatly
// simplifies the case where a body has already been added
// to the request.
req := reqm.(map[string]interface{})
var header http.Header
switch h := req["Header"].(type) {
case nil:
header = make(http.Header)
req["Header"] = header
case map[string][]string:
header = h
case http.Header:
header = h
default:
return types.NewErr("invalid type in header field: %T", h)
}
header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
return types.DefaultTypeAdapter.NativeToValue(req)
}