The Crystal Programming Language Forum

HTTP post a file

What is the best way to post a file with the HTTP::Client?

I have been messing around with some crystal code trying to replicate something like this.

There is a few libraries that make this way easier like Crest and Halite. it would be nice to in the standard library just call a post method with a file like Crest of Halite.

Something like.

response =
  form: { "file" =>"foo.txt") }

It depends on what the server is expecting. If it’s expecting form data you’d be best off using HTTP::FormData::Builder - Crystal 1.0.0-dev and pass the same io to the form argument of .post. Another way would involve sending the endpoint the contents of the file Base64 encoded; such as as part of a JSON object.

It is true it depends on what the server expects. I figured out how to replicate this with HTTP::FormData::Builder I just think there might be an easier interface to do it.

Seems like this might not be something other people want though. I like the interfaces of Crest and Halite though.