6

I use FetchAPI of Fable.PowerPack to make basic authentication application, but I am in trouble because I can not base64 encode.

For example, I wrote such a code,

let base64enc str =
    str
    |> Seq.map byte
    |> Array.ofSeq
    |> System.Convert.ToBase64String

However, the following error occurs.

Error FABLE: Can not find replacement for System.Convert.ToBase64String

Is there any way?

1 Answers1

5

I think the JavaScript btoa function does what you need. The easiest and quickest way to make it available in Fable is to use the Emit attribute:

[<Emit("btoa($0)")>]
let toBase64String (bytes:byte[]) : string = failwith "JS"

There might be some slight differences between the .NET method and the JS function, in which case you'll probably need to reimplement those on your own.

Also, this sounds like a thing that Fable could support - the replacements for .NET operations are generally defined in the Replacements.fs file and if you were interested in sending a pull request to add mapping for ToBase64String and others, that would be amazing!

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553