Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Consume an Azure ML API (Request/Response) using F#
#r @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll"
#r @"..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll"
open System
open System.Net.Http
open System.Net.Http.Headers
open System.Net.Http.Formatting
open System.Collections.Generic
type ScoreData (featureVector:Dictionary<string,string>, globalParameters:Dictionary<string,string>) =
member this.FeatureVector = featureVector
member this.GlobalParameters = globalParameters
type ScoreRequest (id:string, instance:ScoreData) =
member this.Id = id
member this.Instance = instance
let invokeService () = async {
let apiKey = ""
let uri = ""
use client = new HttpClient()
client.DefaultRequestHeaders.Authorization <- new AuthenticationHeaderValue("Bearer",apiKey)
client.BaseAddress <- new Uri(uri)
let input = new Dictionary<string,string>()
input.Add("Zip Code","27519")
input.Add("Voted Ind","1")
let scoreData = new ScoreData(input,new Dictionary<string,string>())
let scoreRequest = new ScoreRequest("score00001",scoreData)
let! response = client.PostAsJsonAsync("",scoreRequest) |> Async.AwaitTask
let! result = response.Content.ReadAsStringAsync() |> Async.AwaitTask
if response.IsSuccessStatusCode then
printfn "%s" result
printfn "FAILED: %s" result
response |> ignore
invokeService() |> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment