Skip to content

Instantly share code, notes, and snippets.

@ijt
Created April 30, 2011 01:08
Show Gist options
  • Save ijt/949308 to your computer and use it in GitHub Desktop.
Save ijt/949308 to your computer and use it in GitHub Desktop.
Example of making a Freebase query from Haskell
import Network.HTTP (simpleHTTP, getRequest, getResponseBody, urlEncode)
main = do
resultJson <- queryFreebase "{\"query\":[{\"id\":null,\"name\":null,\"type\":\"/astronomy/planet\"}]}"
putStrLn resultJson
-- Runs a query against Freebase and returns the result in JSON format.
queryFreebase :: String -> IO String
queryFreebase jsonQuery = do
let query = urlEncode $ jsonQuery
url = "http://api.freebase.com/api/service/mqlread?query=" ++ query
get url
-- Fetches a document and returns it as a String.
get :: String -> IO String
get url = do
let request = getRequest url
response <- simpleHTTP request
getResponseBody response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment