mamantoha / crest

HTTP and REST client for Crystal
https://mamantoha.github.io/crest/
MIT License
235 stars 14 forks source link

Upload file without extension results in exception #173

Closed cyangle closed 2 years ago

cyangle commented 2 years ago

Got below error when try to upload a file without extension.

Unhandled exception: Missing MIME type for extension "" (KeyError)
  from /usr/share/crystal/src/mime.cr:158:33 in 'from_extension'
  from /usr/share/crystal/src/mime.cr:192:5 in 'from_filename'
  from lib/crest/src/crest/forms/data_form.cr:39:7 in 'add_field'
  from lib/crest/src/crest/forms/data_form.cr:20:11 in 'generate'
  from lib/crest/src/crest/form.cr:9:7 in 'generate'
  from lib/crest/src/crest/request.cr:303:14 in 'generate_form_data!'
  from lib/crest/src/crest/request.cr:125:7 in 'initialize:params:headers:form:logging:handle_errors:params_encoder'
  from lib/crest/src/crest/request.cr:159:5 in 'new:params:headers:form:logging:handle_errors:params_encoder'

I think it's reasonable to set a default mime type application/octet-stream when you can not get it from file extension.

Also, it would be nice to be able to upload IO::Memory instead of only File which is possible because HTTP::FormData::Builder#file accepts any instance of IO.

@mamantoha

mamantoha commented 2 years ago

Thank! 🙏