Header menu logo FsHttp

Installing

// Reference the 'FsHttp' package from NuGet in your script or project
#r "nuget: FsHttp"

open FsHttp

Performing a GET request:

http {
    GET "https://mysite"
    AcceptLanguage "en-US"
}
|> Request.send

The request is sent synchronously in the example above. See (TODO: async / task) section to see how requests and responses can be processed using async or task abstractions.

For production use, it is recommended using async or task based functions!

Performing a POST request with JSON object content:

http {
    POST "https://mysite"

    body
    jsonSerialize
        {|
            name = "morpheus"
            job = "leader"
        |}
}
|> Request.send

There are more ways of how requests definition can look: See (here)TODO for an explanation of how to multipart, form data, file upload, streaming, and more.

Process response content as JSON:

// Assume this returns: { "name": "Paul"; "age": 54 }
let name,age =
    http {
        GET "https://mysite"
        AcceptLanguage "en-US"
    }
    |> Request.send
    |> Response.toJson
    |> fun json -> json?name.GetString(), json?age.GetInt32()

Use the ? operator to access JSON properties. The GetString(), GetInt32() and similar methods are used to convert the JSON values to the desired type. They are defined as extension methods in System.Text.Json.JsonElement.

FSharp.Data and Newtonsoft.Json

Per default, System.Text.Json is used as backend for dealing with JSON responses. If prefer FSharp.Data or Newtonsoft.Json, you can use the extension packages (see here(TODO)).

Configuration

// A configuration per request
http {
    GET "https://mysite"
    AcceptLanguage "en-US"

    // This can be placed anywhere in the request definition.
    config_timeoutInSeconds 10.0
}
|> Request.send

There are many ways of configuring a request - from simple config values like above, to changing or replacing the underlying System.Net.Http.HttpClient and System.Net.Http.HttpRequestMessage (have a look here()TODO).

It is also possible to set configuration values globally:

GlobalConfig.defaults
|> Config.timeoutInSeconds 11.1
|> GlobalConfig.set
namespace FsHttp
Multiple items
static member HttpBuilder.http: HeaderContext

--------------------
property HttpBuilder.http: HeaderContext with get
custom operation: GET (string) Calls IRequestContext.Get
custom operation: AcceptLanguage (string) Calls IRequestContext.AcceptLanguage
<summary> List of acceptable human languages for response </summary>
Multiple items
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>
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>
custom operation: jsonSerialize ('a) Calls IRequestContext.JsonSerialize
val name: string
val age: int
Multiple items
module Response from FsHttp.Print

--------------------
module Response from FsHttp

--------------------
type Response = { request: Request requestMessage: HttpRequestMessage content: HttpContent headers: HttpResponseHeaders reasonPhrase: string statusCode: HttpStatusCode version: Version printHint: PrintHint originalHttpRequestMessage: HttpRequestMessage originalHttpResponseMessage: HttpResponseMessage ... } interface IDisposable interface IUpdatePrintHint<Response>
val toJson: response: Response -> System.Text.Json.JsonElement
val json: System.Text.Json.JsonElement
custom operation: config_timeoutInSeconds (float) Calls IRequestContext.TimeoutInSeconds
module GlobalConfig from FsHttp
val defaults: GlobalConfig.GlobalConfigWrapper
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) ... }
val timeoutInSeconds: value: float -> context: IUpdateConfig<'a> -> 'a
val set: config: GlobalConfig.GlobalConfigWrapper -> unit

Type something to start searching.