Skip to content

granoctigo/Fable.SimpleHttp

 
 

Repository files navigation

Fable.SimpleHttp Build Status Build status Nuget

A library for easily working with Http in Fable projects.

Features

  • Extremely simple API for working with HTTP requests and responses.
  • Implemented in idiomatic F# Async (instead of promises which follow JS semantics)
  • Supports sending and receiving raw binary data (i.e.Blob in the browser)
  • Built on top of XMLHttpRequest available in all browsers (even IE11!) so it doesn't need the Fetch API nor it's associated polyfill.

Installation

Install from nuget using paket

paket add nuget Fable.SimpleHttp --project path/to/YourProject.fsproj

Usage

open Fable.SimpleHttp

// Functions from the Http module are all safe and do not throw exceptions

// GET request

async {
    let! (statusCode, responseText) = Http.get "/api/data"

    match statusCode with
    | 200 -> printfn "Everything is fine => %s" responseText
    | _ -> printfn "Status %d => %s" statusCode responseText
}

// POST request

async {
    let requestData = "{ \"id\": 1 }"
    let! (statusCode, responseText) = Http.post "/api/echo" requestData
    printfn "Server responded => %s" responseText
}

// Fully configurable request
async {
    let! response =
        Http.request "/api/data"
        |> Http.method POST
        |> Http.content (BodyContent.Text "{ }")
        |> Http.header (Headers.contentType "application/json")
        |> Http.header (Headers.authorization "Bearer <token>")
        |> Http.send

    printfn "Status: %d" response.statusCode
    printfn "Content: %s" response.responseText

    // response headers are lower cased
    response.responseHeaders
    |> Map.tryFind "content-length"
    |> Option.map int
    |> Option.iter (printfn "Content length: %d")
}


// Sending form data
async {
    let formData =
        FormData.create()
        |> FormData.append "firstName" "Zaid"
        |> FormData.append "lastName" "Ajaj"

    let! response =
        Http.request "/api/echo-form"
        |> Http.method POST
        |> Http.content (BodyContent.Form formData)
        |> Http.send

    printfn "Status => %d" response.statusCode
}


// Send and receive binary data with Blobs
// use FileReader module
async {
    let blob = Blob.fromText "input data"

    let! response =
       Http.request "/api/echoBinary"
       |> Http.method POST
       |> Http.content (BodyContent.Binary blob)
       |> Http.overrideResponseType ResponseTypes.Blob
       |> Http.send

    match response.content with
    | ResponseContent.Blob content ->
        let! blobContent = FileReader.readBlobAsText content
        printfn "Received content: %s" blobContent // "Received content: input data"

    | _ ->
        printfn "Unexpected response content"
}

Building and running tests

Requirements

  • Dotnet core 2.1+
  • Mono 5.0+
  • Node 10.0+

Watch mode: both client and server live

./build.sh Start

Running the the end-to-end tests

./build.sh Test

or just Ctrl + Shift + B to run the cli tests as a VS Code task

About

Http with Fable, made simple.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • F# 92.2%
  • JavaScript 5.3%
  • Shell 1.6%
  • Other 0.9%