Skip to content

Instantly share code, notes, and snippets.

@Thorium
Created May 20, 2016 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thorium/7b00397e4efb3028e664dd359437de7e to your computer and use it in GitHub Desktop.
Save Thorium/7b00397e4efb3028e664dd359437de7e to your computer and use it in GitHub Desktop.
Send async HTTP POST request
open System.Net
open System.IO
let makePostRequest (url : string) (requestBody : string) =
let req = WebRequest.CreateHttp url
req.CookieContainer <- new CookieContainer()
req.Method <- "POST"
req.ProtocolVersion <- HttpVersion.Version10
let postBytes = requestBody |> System.Text.Encoding.ASCII.GetBytes
req.ContentLength <- postBytes.LongLength
req.ContentType <- "application/xml; charset=utf-8"
async{
use! reqStream = req.GetRequestStreamAsync() |> Async.AwaitTask
do! reqStream.WriteAsync(postBytes, 0, postBytes.Length) |> Async.AwaitIAsyncResult |> Async.Ignore
reqStream.Close()
use! res = req.AsyncGetResponse()
use stream = res.GetResponseStream()
use reader = new StreamReader(stream)
let! rdata = reader.ReadToEndAsync() |> Async.AwaitTask
return rdata
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment