I'm trying to use the XmlProvider
of FSharp.Data
and I've run into a smaller problem. The API I'm working against requires an HTTP Header to be sent containing Accept
and Accept-Language
. Now if I do this..
let afRequestString url =
Http.RequestString(
url,
httpMethod="GET",
headers = [ Accept HttpContentTypes.Xml; AcceptLanguage "sv-se,sv" ])
That will work fine, unfortunately I can't make this work directly with the XmlProvider like this type foo = XmlProvider<afRequestString "bar">
and this won't work either:
let foo = afRequestString "bar"
type bar = XmlProvider<foo>
The complaint from the compiler is that the Sample
provided to the XmlProvider
must be static and constant, which is sort of weird to me because from what I understand let-bindings are immutable so they are in nature constant.
Anyhow the only thing that will work that I have discovered is doing it this way
File.WriteAllText("foo.xml", afRequestString "foo") |> ignore
type bar = XmlProvider<"foo.xml">
But this really seems like an excessively complicated way of doing this. Isn't there a simpler way to do this?
When using XML type provider (and other type providers), the sample must be known at compile-time. This means that you cannot use a value that is a result of some (more or less complex call).
If you need to specify things like headers to get the sample, then the best option is to get the sample file first (in some way) and save it to a local file (or you can get the file as part of your build script, if you always want to have an up-to-date version).
The XmlProvider
has a couple of parameters that specify how to get the sample and I suspect that adding more configuration for HTTP request would be reasonable, but saving the file locally should usually be good enough...