Skip to content

Add ToRequestBody RequestBody instance #11

@pete-murphy

Description

@pete-murphy

I was hoping to use fetch in Node, and to pass a Buffer as request body, but this is not currently possible.

main :: Effect Unit
main = do
  buffer <- Buffer.fromString "Hello, world" UTF8
  Aff.launchAff_ do
    void (Fetch.fetchBody "http://example.com" { body: buffer })

This fails to compile because there's no ToRequestBody instance for Buffer.

I opened an issue in web-fetch purescript-web/purescript-web-fetch#15 about adding RequestBody.fromBuffer (which would enable adding a ToRequestBody Buffer instance here) but that's not the right approach as it would incur a dependency on node-buffer and make both the "core" web-fetch and this fetch not portable between environments.

I think maybe a better solution would be to have instance ToRequestBody RequestBody in this library, and then a hypothetical node-fetch could wrap this fetch implementation and have its own RequestBody module with Node-specific conversion functions like fromBuffer :: Buffer -> RequestBody:

main :: Effect Unit
main = do
  buffer <- Buffer.fromString "Hello, world" UTF8
  Aff.launchAff_ do
    void (Fetch.fetchBody "http://example.com" { body: RequestBody.fromBuffer buffer })

Let me know if that makes sense, or if there are reasons for omitting the instance ToRequestBody RequestBody that I'm over-looking. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions