Simplest Way for a newbie to make API calls

I googled this subject and found this thread. So from what I understand is that ReScript natively doesn’t provide a way to make API calls and we have to chose one from literally 100s of libraries to make API calls.

My question is which is the most simple way for a newbie to make API calls? and what are the basic concepts to understand here? Note that I don’t know JavaScript and I don’t have a web development background. I have only attended tutorials on HTML, CSS, Webpack, and React and Re-Script.

Please point me to basic concepts which I should learn to be able to make API calls and library which is “simple enough for a 5 year old” person to make API calls from rescript.

I feel the simplest way to make a request is by using fetch

And ReScript is a language which compiles to javascript. So we would need to write some bindings for this javascript fetch method.

eg: playground

type response
@val external fetch: string => promise<response> = "fetch"
@send external json: response => promise<'a> = "json"

let makeRequest = async url => {
  let response = await fetch(url)
  let json = await response->json
  Js.log(json)
}
1 Like

yes but in that thread Fetch was discouraged and XMLHTTPRequest looked hard. so I was wondering if there is a middle ground somewhere?

It’s arguable. Fetch is fine in the most cases. You’ll know yourself if you need XMLHTTPRequest. Also, for fetch there’s GitHub - glennsl/rescript-fetch: Experimental zero-cost rescript bindings to the WHATWG Fetch API

4 Likes

Was :slight_smile: I’d say the difference matters if you care about request cancellation and still support IE11. Otherwise, rescript-fetch that DZakh linked to above even has bindings for AbortController.

5 Likes

Thanks everyone rescript-fetch it is then.

1 Like