thenables / requisition

ES7 async/await ready http client
MIT License
57 stars 3 forks source link

Discussion: implement formData body support? #20

Open drawveloper opened 8 years ago

drawveloper commented 8 years ago

We could implement support for multipart/form-data by being compatible with a form-data-like body. Something like checking for a getHeaders function?

jonathanong commented 8 years ago

i personally hate multipart. is this for request or response body?

haoxins commented 8 years ago

I dislike multipart too. But it's useful. 😢 (For me, I have to request some APIs by form-data)

BTW, https://github.com/visionmedia/superagent support multipart/form-data. 😄

jonathanong commented 8 years ago

@firstdoit would you like to be a maintainer? this module does basically everything i need already, so i'm not really actively maintaining it

drawveloper commented 8 years ago

@jonathanong that would be lovely. This module is generally very good, but lacks a couple of tiny features that would really make it shine in my opinion. I started using it because of it's well-behaved size/amount of deps. Comparing this to request-promise:

npm i request request-promise
cloc node_modules/
    1723 text files.
    1659 unique files.
     290 files ignored.

https://github.com/AlDanial/cloc v 1.66  T=4.18 s (343.4 files/s, 30287.5 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JavaScript                    1267          13850          27276          65065
JSON                           130             38              0          17638
XML                              2            233             11           1948
YAML                            24             21             23            244
make                             7             27             28             64
Bourne Shell                     1              5              2             27
HTML                             2              2              0             27
C                                1              7              4             23
Lisp                             1              0              0              6
-------------------------------------------------------------------------------
SUM:                          1435          14183          27344          85042
———————————————————————————————————————
npm i requisition
cloc node_modules/
     196 text files.
     176 unique files.
      80 files ignored.

https://github.com/AlDanial/cloc v 1.66  T=0.53 s (217.2 files/s, 26757.2 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
JSON                            28              0              0           9604
JavaScript                      81            693            836           3047
TypeScript                       4             13             37             44
YAML                             2              0              0             12
make                             1              1              0              3
-------------------------------------------------------------------------------
SUM:                           116            707            873          12710
———————————————————————————————————————
drawveloper commented 8 years ago

As for form-data support, I believe we could do it without incurring in any extra deps: just document our support for form-data-like API. The idea would be for the user to create their own FormData object and pass it as body:

import FormData from 'form-data'
import request from 'requisition'
import {createReadStream} from 'fs'

const form = new FormData()
form.append('my_field', 'my value')
form.append('my_buffer', new Buffer(10))
form.append('my_file', createReadStream('/foo/bar.jpg'))

request.post('my/url/receiving/multipart').send(form)

Or something like that.