3 people like it.

Send async HTTP POST request

Bit like http://www.fssnip.net/a7 but send as async.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
#if INTERACTIVE
#r "System.Net.Http.dll"
#endif

open System.Net
open System.IO

type PostRequestType =
| Text
| ApplicationXml
| ApplicationSoapXml
| ApplicationJson

let timeout = 50000 //ms
/// Make a post-web-request, with custom headers
let makePostRequestWithHeaders (reqType: PostRequestType) (url : string) (requestBody : string) (headers) =
    let req = WebRequest.CreateHttp url
    headers |> Seq.iter(fun (h:string,k:string) -> req.Headers.Add(h,k))
    req.CookieContainer <- new CookieContainer()
    req.Method <- "POST"
    req.Timeout <- timeout
    req.ProtocolVersion <- HttpVersion.Version10
    let postBytes = requestBody |> System.Text.Encoding.ASCII.GetBytes
    req.ContentLength <- postBytes.LongLength
    let reqtype =
        match reqType with
        | Text -> "text/xml; charset=utf-8"
        | ApplicationXml -> "application/xml; charset=utf-8"
        | ApplicationSoapXml -> "application/soap+xml; charset=utf-8"
        | ApplicationJson -> "application/json"
    req.ContentType <- reqtype
    async {
        let! res =
            async{
                let! reqStream = req.GetRequestStreamAsync() |> Async.AwaitTask
                do! reqStream.WriteAsync(postBytes, 0, postBytes.Length) |> Async.AwaitIAsyncResult |> Async.Ignore
                reqStream.Close()
                let! res =
                    async { // Async methods are not using req.Timeout
                        let! child = Async.StartChild(req.AsyncGetResponse(), timeout)
                        return! child
                    }
                use stream = res.GetResponseStream()
                use reader = new StreamReader(stream)
                let! rdata = reader.ReadToEndAsync() |> Async.AwaitTask
                return rdata
            } |> Async.Catch
        match res with
        | Choice1Of2 x -> return x
        | Choice2Of2 e ->
            match e with
            | :? WebException as wex ->
                // Read the error from response
                use stream = wex.Response.GetResponseStream()
                use reader = new StreamReader(stream)
                let err = reader.ReadToEnd()
                return failwith err
            | _ ->
                // System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(e).Throw()
                return failwith(e.ToString())
    }
    //  |> Async.StartAsTask |> Task.WaitAll

/// Make a post-web-request
let makePostRequest (reqType: PostRequestType) (url : string) (requestBody : string) =
    makePostRequestWithHeaders reqType url requestBody [||]
namespace System
namespace System.Net
namespace System.IO
type PostRequestType =
  | Text
  | ApplicationXml
  | ApplicationSoapXml
  | ApplicationJson

Full name: Script.PostRequestType
union case PostRequestType.Text: PostRequestType
union case PostRequestType.ApplicationXml: PostRequestType
union case PostRequestType.ApplicationSoapXml: PostRequestType
union case PostRequestType.ApplicationJson: PostRequestType
val timeout : int

Full name: Script.timeout
val makePostRequestWithHeaders : reqType:PostRequestType -> url:string -> requestBody:string -> headers:seq<string * string> -> Async<'a>

Full name: Script.makePostRequestWithHeaders


 Make a post-web-request, with custom headers
val reqType : PostRequestType
val url : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val requestBody : string
val headers : seq<string * string>
val req : obj
type WebRequest =
  inherit MarshalByRefObject
  member Abort : unit -> unit
  member AuthenticationLevel : AuthenticationLevel with get, set
  member BeginGetRequestStream : callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginGetResponse : callback:AsyncCallback * state:obj -> IAsyncResult
  member CachePolicy : RequestCachePolicy with get, set
  member ConnectionGroupName : string with get, set
  member ContentLength : int64 with get, set
  member ContentType : string with get, set
  member Credentials : ICredentials with get, set
  member EndGetRequestStream : asyncResult:IAsyncResult -> Stream
  ...

Full name: System.Net.WebRequest
module Seq

from Microsoft.FSharp.Collections
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val h : string
val k : string
Multiple items
type CookieContainer =
  new : unit -> CookieContainer + 2 overloads
  member Add : cookie:Cookie -> unit + 3 overloads
  member Capacity : int with get, set
  member Count : int
  member GetCookieHeader : uri:Uri -> string
  member GetCookies : uri:Uri -> CookieCollection
  member MaxCookieSize : int with get, set
  member PerDomainCapacity : int with get, set
  member SetCookies : uri:Uri * cookieHeader:string -> unit
  static val DefaultCookieLimit : int
  ...

Full name: System.Net.CookieContainer

--------------------
CookieContainer() : unit
CookieContainer(capacity: int) : unit
CookieContainer(capacity: int, perDomainCapacity: int, maxCookieSize: int) : unit
Multiple items
type HttpVersion =
  new : unit -> HttpVersion
  static val Version10 : Version
  static val Version11 : Version

Full name: System.Net.HttpVersion

--------------------
HttpVersion() : unit
field HttpVersion.Version10
val postBytes : byte []
namespace System.Text
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
property System.Text.Encoding.ASCII: System.Text.Encoding
System.Text.Encoding.GetBytes(s: string) : byte []
System.Text.Encoding.GetBytes(chars: char []) : byte []
System.Text.Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
System.Text.Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
System.Text.Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
System.Text.Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
property System.Array.LongLength: int64
val reqtype : string
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val res : Choice<'a,exn>
val reqStream : obj
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task -> Async<unit>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken

Full name: Microsoft.FSharp.Control.Async

--------------------
type Async<'T>

Full name: Microsoft.FSharp.Control.Async<_>
static member Async.AwaitTask : task:System.Threading.Tasks.Task -> Async<unit>
static member Async.AwaitTask : task:System.Threading.Tasks.Task<'T> -> Async<'T>
property System.Array.Length: int
static member Async.AwaitIAsyncResult : iar:System.IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member Async.Ignore : computation:Async<'T> -> Async<unit>
val res : obj
val child : Async<obj>
static member Async.StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
val stream : System.IDisposable
val reader : StreamReader
Multiple items
type StreamReader =
  inherit TextReader
  new : stream:Stream -> StreamReader + 9 overloads
  member BaseStream : Stream
  member Close : unit -> unit
  member CurrentEncoding : Encoding
  member DiscardBufferedData : unit -> unit
  member EndOfStream : bool
  member Peek : unit -> int
  member Read : unit -> int + 1 overload
  member ReadLine : unit -> string
  member ReadToEnd : unit -> string
  ...

Full name: System.IO.StreamReader

--------------------
StreamReader(stream: Stream) : unit
StreamReader(path: string) : unit
StreamReader(stream: Stream, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(stream: Stream, encoding: System.Text.Encoding) : unit
StreamReader(path: string, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(path: string, encoding: System.Text.Encoding) : unit
StreamReader(stream: Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
StreamReader(stream: Stream, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
StreamReader(path: string, encoding: System.Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
val rdata : 'a
static member Async.Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
union case Choice.Choice1Of2: 'T1 -> Choice<'T1,'T2>
val x : 'a
union case Choice.Choice2Of2: 'T2 -> Choice<'T1,'T2>
val e : exn
Multiple items
type WebException =
  inherit InvalidOperationException
  new : unit -> WebException + 4 overloads
  member GetObjectData : serializationInfo:SerializationInfo * streamingContext:StreamingContext -> unit
  member Response : WebResponse
  member Status : WebExceptionStatus

Full name: System.Net.WebException

--------------------
WebException() : unit
WebException(message: string) : unit
WebException(message: string, innerException: exn) : unit
WebException(message: string, status: WebExceptionStatus) : unit
WebException(message: string, innerException: exn, status: WebExceptionStatus, response: WebResponse) : unit
val wex : WebException
val stream : Stream
property WebException.Response: WebResponse
WebResponse.GetResponseStream() : Stream
val err : string
StreamReader.ReadToEnd() : string
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
System.Exception.ToString() : string
val makePostRequest : reqType:PostRequestType -> url:string -> requestBody:string -> Async<'a>

Full name: Script.makePostRequest


 Make a post-web-request

More information

Link:http://fssnip.net/7PK
Posted:6 years ago
Author:Tuomas Hietanen
Tags: http , request , web , webrequest , post