Coder Social home page Coder Social logo

orfeasz / fable.simplehttp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zaid-ajaj/fable.simplehttp

0.0 1.0 0.0 17.18 MB

Http with Fable, made simple.

License: MIT License

Shell 1.63% JavaScript 5.31% F# 92.19% HTML 0.38% Batchfile 0.50%

fable.simplehttp's Introduction

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

fable.simplehttp's People

Contributors

zaid-ajaj avatar dependabot[bot] avatar stroborobo avatar orfeasz avatar sergey-tihon avatar alfonsogarciacaro avatar adelarsq avatar okayx6 avatar nosami avatar kspeakman avatar kerams avatar njlr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.