Coder Social home page Coder Social logo

fetch's Introduction

Fetch

GitHub release Discord chat test

Bindings to JavaScript's built in HTTP client, fetch.

If you are running your Gleam project on the Erlang target (the default for new Gleam projects) then you will want to use a different library which can run on Erlang, such as gleam_httpc.

import gleam/fetch
import gleam/http/request
import gleam/http/response
import gleam/javascript/promise

pub fn main() {
  let assert Ok(req) = request.to("https://example.com")

  // Send the HTTP request to the server
  use resp <- promise.try_await(fetch.send(req))
  use resp <- promise.try_await(fetch.read_text_body(resp))

  // We get a response record back
  resp.status
  // -> 200

  response.get_header(resp, "content-type")
  // -> Ok("text/html; charset=UTF-8")

  promise.resolve(Ok(Nil))
}

fetch's People

Contributors

chuckwondo avatar crowdhailer avatar lpil avatar michallepicki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fetch's Issues

Passing options when sending fetch requests

Some time ago we chatted how we could make it possible to pass request options to fetch.send(). I actually forgot about the issue until I hopped back on my toy project this weekend and got stuck ๐Ÿ˜† Opening a GH issue this time to keep it visible.

Here's the related discord discussion. We didn't reach any conclusion but it should help jog our memory.

The simplest solution would be to introduce a FetchOptions type that we would pass to a new dedicated function like fetch.send_with_options(request, options).

Another path would be having a new FetchRequest type that wraps the classic gleam/http/request.{Request} with additional fetch-specific fields, such as mode, credentials, etc. We could then provide a builder pipeline to take the request, add options, and send it.

Support for binary content

3rd party provides me with large gzip'ed json.

Add

@external(javascript, "../ffi.mjs", "read_body")
pub fn read_body(
  a: Response(FetchBody),
) -> Promise(Result(Response(BitArray), FetchError))

which the user must gunzip afterwards?

If so, I have a PR.

README example leads to errors

Hello, I am learning gleam and wanted to figure out how to make http calls in JS targets but the README example produces an error and I am not sure what to do.
Here are my steps:

  1. $ gleam new fetch
  2. $ cd fetch
  3. $ gleam add gleam_fetch
  4. copy and paste the code example from the current README
  5. $ gleam run -t javascript

The result:

Compiled in 0.02s
    Running fetch.main
file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/ffi.mjs:40
  let headers = new globalThis.Headers();
                ^

TypeError: globalThis.Headers is not a constructor
    at make_headers (file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/ffi.mjs:40:17)
    at to_fetch_request (file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/ffi.mjs:32:14)
    at Module.send (file:///Users/george/dev/gleam/fetch/build/dev/javascript/gleam_fetch/gleam/fetch.mjs:37:17)
    at main (file:///Users/george/dev/gleam/fetch/build/dev/javascript/fetch/fetch.mjs:21:12)
    at file:///Users/george/dev/gleam/fetch/build/dev/javascript/fetch/gleam.main.mjs:2:1
    at ModuleJob.run (node:internal/modules/esm/module_job:175:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)

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.