I am making the following request which is returning Json.
let baseUrl = "http://wex-qa.mybluemix.net/resources/question"
let userName = "[email protected]"
let password = "yourCreds"
let authKey = userName + ":" + password
let client = new HttpClient()
client.DefaultRequestHeaders.Authorization <- new AuthenticationHeaderValue("Basic",authKey)
let input = new Dictionary<string,string>()
input.Add("question","what time is it")
let content = new FormUrlEncodedContent(input)
let result = client.PostAsync(baseUrl,content).Result
let resultContent = result.Content.ReadAsStringAsync().Result
I immediately thought of using the Json Type Provider so I made a local file of the response to be the type def. I then went to load the type where I need the credentials and the content. However, I did not see where the .Load() function have an overload to pass in the credentials and the content. Should I continue to make the request via the HttpClient class and use the .Parse() function?
Thanks in advance.
Expanding on the answer in the comments (which is correct).
I had this problem in 2022 with FSharp.Data 4.2.9. There still seems to be no way to put authorisation credentials into the requests for either defining the
JsonProvidertype, or the runtime collection of data. With a bit of working around, it can be made to work like a type provider, as described in the question and comment.Parsemethod of the type provide to return typed data. This allows you to use intellisense on the returned dataThis example is for the MailChimp API, which supports mailing lists and requires a company
API Keyon web requests.First, follow instructions on the site to get 'Credentials' for API calls.
Second, use any method to extract sample JSON to a file. I used the PHP samples provided by MailChimp.
Then define the
JsonProviderwith that data.Here
listsis a fundamental component of the API. In this example, I will be working with the "Sales Subscribers" list.The next function will read from a URL, and return typed Json data, as if directly from the
Loadfunction.The core of this is the jsonProviderParse argument, which is the
Parsemethod from the JsonProvider.Then use
loadTypedJsonas you wouldmyTypeProvider.Load("https:..")Output: list_id = f21XXXXX85
Intellisense working for the JsonProvider