Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zaid-ajaj/fable.simplehttp
Http with Fable, made simple.
https://github.com/zaid-ajaj/fable.simplehttp
ajax fable fsharp http request
Last synced: 13 days ago
JSON representation
Http with Fable, made simple.
- Host: GitHub
- URL: https://github.com/zaid-ajaj/fable.simplehttp
- Owner: Zaid-Ajaj
- License: mit
- Created: 2018-10-31T00:44:00.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-11-01T11:42:17.000Z (about 1 year ago)
- Last Synced: 2024-09-17T01:53:56.846Z (about 2 months ago)
- Topics: ajax, fable, fsharp, http, request
- Language: F#
- Size: 16.8 MB
- Stars: 89
- Watchers: 5
- Forks: 12
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fable.SimpleHttp [![Build Status](https://travis-ci.org/Zaid-Ajaj/Fable.SimpleHttp.svg?branch=master)](https://travis-ci.org/Zaid-Ajaj/Fable.SimpleHttp) [![Build status](https://ci.appveyor.com/api/projects/status/fgbd40ahcyrec5uw?svg=true)](https://ci.appveyor.com/project/Zaid-Ajaj/fable-simplehttp) [![Nuget](https://img.shields.io/nuget/v/Fable.SimpleHttp.svg?maxAge=0&colorB=brightgreen)](https://www.nuget.org/packages/Fable.SimpleHttp)
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](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) nor it's associated polyfill.### Installation
Install from nuget using paket
```sh
paket add nuget Fable.SimpleHttp --project path/to/YourProject.fsproj
```## Usage
```fs
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 ")
|> Http.sendprintfn "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.sendprintfn "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.sendmatch 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
```sh
./build.sh Start
```
Running the the end-to-end tests
```sh
./build.sh Test
```
or just `Ctrl + Shift + B` to run the cli tests as a VS Code task