JSON String
http {
POST "https://mysite"
body
json """
{
"name" = "morpheus",
"job" = "leader",
"age": %d
}
"""
}
|> Request.send
Parametrized JSON:
When the JSON content needs to be parametrized, sprintf
function is a useful tool. Compared to interpolated strings, the curly braces - which are a key character in JSON - don't have to be escaped:
let sendRequestWithSprintf age =
http {
POST "https://mysite"
body
json (sprintf """
{
"name" = "morpheus",
"job" = "leader",
"age": %d
}
""" age)
}
|> Request.send
Using an interpolated string:
let sendRequestWithInterpolatedString age =
http {
POST "https://mysite"
body
json $"""
{{
"name" = "morpheus",
"job" = "leader",
"age": {age}
}}
"""
}
|> Request.send
Sending records or objects as JSON
http {
POST "https://mysite"
body
jsonSerialize
{|
name = "morpheus"
job = "leader"
|}
}
|> Request.send
It is also possible to pass serializer settings using the
jsonSerializeWith
operation.
Plain text
http {
POST "https://mysite"
body
// Sets Content-Type: plain/text header
text """
The last train is nearly due
The underground is closing soon
And in the dark deserted station
Restless in anticipation
A man waits in the shadows
"""
}
|> Request.send
Request Cancellation
It is possible to bind a cancellation token to a request definition, that will be used for the underlying HTTP request:
use cts = new CancellationTokenSource()
// ...
http {
GET "https://mysite"
config_cancellationToken cts.Token
}
|> Request.send
See also: https://github.com/fsprojects/FsHttp/issues/105
Instead of binding a cancellation token directly to a request definition (like in the example above), it is also possible to pass it on execution-timer, like so:
http {
GET "https://mysite"
}
|> Config.cancellationToken cts.Token
|> Request.send
namespace FsHttp
namespace System
namespace System.Threading
Multiple items
static member HttpBuilder.http: HeaderContext
--------------------
property HttpBuilder.http: HeaderContext with get
static member HttpBuilder.http: HeaderContext
--------------------
property HttpBuilder.http: HeaderContext with get
custom operation: POST (string)
Calls IRequestContext.Post
custom operation: body
Calls IRequestContext.Body
<summary> An explicit transformation from a previous context to allow for describing the request body. </summary>
<summary> An explicit transformation from a previous context to allow for describing the request body. </summary>
custom operation: json (string)
Calls IRequestContext.Json
Multiple items
module Request from FsHttp.Print
--------------------
module Request from FsHttp
--------------------
type Request = { header: Header content: BodyContent config: Config printHint: PrintHint }
module Request from FsHttp.Print
--------------------
module Request from FsHttp
--------------------
type Request = { header: Header content: BodyContent config: Config printHint: PrintHint }
val send: request: IToRequest -> Response
<summary> Sends a request synchronously. </summary>
<summary> Sends a request synchronously. </summary>
val sendRequestWithSprintf: age: int -> Response
val age: int
val sprintf: format: Printf.StringFormat<'T> -> 'T
val sendRequestWithInterpolatedString: age: 'a -> Response
val age: 'a
custom operation: jsonSerialize ('a)
Calls IRequestContext.JsonSerialize
custom operation: text (string)
Calls IRequestContext.Text
val cts: CancellationTokenSource
Multiple items
type CancellationTokenSource = interface IDisposable new: unit -> unit + 3 overloads member Cancel: unit -> unit + 1 overload member CancelAfter: millisecondsDelay: int -> unit + 1 overload member CancelAsync: unit -> Task member Dispose: unit -> unit member TryReset: unit -> bool static member CreateLinkedTokenSource: token: CancellationToken -> CancellationTokenSource + 2 overloads member IsCancellationRequested: bool member Token: CancellationToken
<summary>Signals to a <see cref="T:System.Threading.CancellationToken" /> that it should be canceled.</summary>
--------------------
CancellationTokenSource() : CancellationTokenSource
CancellationTokenSource(millisecondsDelay: int) : CancellationTokenSource
CancellationTokenSource(delay: System.TimeSpan) : CancellationTokenSource
CancellationTokenSource(delay: System.TimeSpan, timeProvider: System.TimeProvider) : CancellationTokenSource
type CancellationTokenSource = interface IDisposable new: unit -> unit + 3 overloads member Cancel: unit -> unit + 1 overload member CancelAfter: millisecondsDelay: int -> unit + 1 overload member CancelAsync: unit -> Task member Dispose: unit -> unit member TryReset: unit -> bool static member CreateLinkedTokenSource: token: CancellationToken -> CancellationTokenSource + 2 overloads member IsCancellationRequested: bool member Token: CancellationToken
<summary>Signals to a <see cref="T:System.Threading.CancellationToken" /> that it should be canceled.</summary>
--------------------
CancellationTokenSource() : CancellationTokenSource
CancellationTokenSource(millisecondsDelay: int) : CancellationTokenSource
CancellationTokenSource(delay: System.TimeSpan) : CancellationTokenSource
CancellationTokenSource(delay: System.TimeSpan, timeProvider: System.TimeProvider) : CancellationTokenSource
custom operation: GET (string)
Calls IRequestContext.Get
custom operation: config_cancellationToken (CancellationToken)
Calls IRequestContext.CancellationToken
property CancellationTokenSource.Token: CancellationToken with get
<summary>Gets the <see cref="T:System.Threading.CancellationToken" /> associated with this <see cref="T:System.Threading.CancellationTokenSource" />.</summary>
<exception cref="T:System.ObjectDisposedException">The token source has been disposed.</exception>
<returns>The <see cref="T:System.Threading.CancellationToken" /> associated with this <see cref="T:System.Threading.CancellationTokenSource" />.</returns>
<summary>Gets the <see cref="T:System.Threading.CancellationToken" /> associated with this <see cref="T:System.Threading.CancellationTokenSource" />.</summary>
<exception cref="T:System.ObjectDisposedException">The token source has been disposed.</exception>
<returns>The <see cref="T:System.Threading.CancellationToken" /> associated with this <see cref="T:System.Threading.CancellationTokenSource" />.</returns>
Multiple items
module Config from FsHttp.Dsl
--------------------
type Config = { timeout: TimeSpan option defaultDecompressionMethods: DecompressionMethods list headerTransformers: (Header -> Header) list httpMessageTransformers: (HttpRequestMessage -> HttpRequestMessage) list httpClientHandlerTransformers: (SocketsHttpHandler -> SocketsHttpHandler) list httpClientTransformers: (HttpClient -> HttpClient) list httpCompletionOption: HttpCompletionOption proxy: Proxy option certErrorStrategy: CertErrorStrategy httpClientFactory: (Config -> HttpClient) ... }
module Config from FsHttp.Dsl
--------------------
type Config = { timeout: TimeSpan option defaultDecompressionMethods: DecompressionMethods list headerTransformers: (Header -> Header) list httpMessageTransformers: (HttpRequestMessage -> HttpRequestMessage) list httpClientHandlerTransformers: (SocketsHttpHandler -> SocketsHttpHandler) list httpClientTransformers: (HttpClient -> HttpClient) list httpCompletionOption: HttpCompletionOption proxy: Proxy option certErrorStrategy: CertErrorStrategy httpClientFactory: (Config -> HttpClient) ... }
val cancellationToken: cancellationToken: CancellationToken -> context: IUpdateConfig<'a> -> 'a