I my last article I introduced React Native app development with F#Â and mentioned briefly that we can do HTTP calls with the help of the very popular Fetch API.
Fetch provides a generic definition of Request and Response objects (and other things involved with network requests). This will allow them to be used wherever they are needed in the future, whether it’s for service workers, Cache API and other similar things that handle or modify requests and responses, or any kind of use case that might require you to generate your own responses programmatically. [project site]
Dave Thomas created a Fable bindings project called fable-fetch-import. This npm package allows us to use Fetch in JavaScript apps from F#.
Data access
Retrieving data from the web with Fetch is really easy. In the following snippet we just pull a bit of JSON from a HTTP resource and cast it to a custom type.
As you can see all JavaScript Promises are already mapped into F#’s async type which makes it really easy to use.
HTTP Post
If you want to post data to a HTTP resource you can do this like in the following sample:
Going further
If you need to do more complicated calls then take a look into the Fetch docs. Many of the properties and functions are already available in the Fable bindings. If not please create an issue at the Fable issue tracker and I will try to add it.