Last active
March 27, 2020 11:24
-
-
Save awright18/d4873a8ff3b018181b7ce89586d5144e to your computer and use it in GitHub Desktop.
F# http reqeust with HttpClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Learn more about F# at http://fsharp.org | |
open System | |
open System.Text | |
open System.Net.Http | |
open Rss | |
[<EntryPoint>] | |
let main argv = | |
let doXmlStuff = async { | |
let client = new HttpClient() | |
let! response = client.GetByteArrayAsync("http://pablojuan.com/feed/") | |
|> Async.AwaitTask | |
let content = Encoding.UTF8.GetString(response,0, (response.Length - 1)) | |
let parsedStuff = deserializeXml content | |
do printfn "%s" parsedStuff.channel.title | |
return () | |
} | |
doXmlStuff |> Async.RunSynchronously | |
0 // return an integer exit code |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Rss | |
open System.IO | |
open System.Text | |
open System.Xml | |
open System.Xml.Serialization | |
[<CLIMutable>] | |
type RssItem = { | |
title:string; | |
link:string; | |
description:string; | |
author:string; | |
category:string option; | |
enclosure: string option; | |
guid: string option; | |
pubDate: string option; | |
source: string option; | |
} | |
[<CLIMutable>] | |
type RssChannel = { | |
title:string; | |
link:string; | |
description:string; | |
language: string option; | |
copyright: string option; | |
managingEditor: string option; | |
webMaster: string option; | |
pubDate: string option; | |
lastBuildDate: string option; | |
category:string option; | |
generator:string option; | |
docs: string option; | |
cloud: string option; | |
ttl: string option; | |
image: string option; | |
textInput: string option; | |
skipHours: string option; | |
skipDays: string option; | |
item: RssItem[]; | |
} | |
[<CLIMutable>] | |
type Rss = {channel: RssChannel} | |
let deserializeXml str = | |
let root = XmlRootAttribute("rss") | |
let serializer = XmlSerializer(typeof<Rss>,root) | |
use reader = new StringReader(str) | |
serializer.Deserialize reader :?> Rss |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="FSharp.NET.Sdk;Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>netcoreapp1.1</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<Compile Include="rss.fs" /> | |
<Compile Include="Program.fs" /> | |
</ItemGroup> | |
<ItemGroup> | |
<PackageReference Include="FSharp.Core" Version="4.1.*" /> | |
<PackageReference Include="FSharp.NET.Sdk" Version="1.0.*" PrivateAssets="All" /> | |
<!-- You need to add this for System.Net.Http to work. After adding this do a dotnet restore --> | |
<PackageReference Include="System.Net.Http" Version="4.3.*" /> | |
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.*" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will now take this code and port it over to Nikeza. Thanks to all who helped!