babashka / babashka.curl

A This library is mostly replaced by https://github.com/babashka/http-client
Eclipse Public License 1.0
121 stars 9 forks source link

Provide an example of the equivalent of curl -F option #4

Closed bzg closed 4 years ago

bzg commented 4 years ago

First of all, nice project (among other), thanks for sharing it!

I recently had to use curl -H ... -F file=@/path/to/file in a Clojure project, relying on clojure.java.shell for this (see the code here).

Would it make sense to use babashka.curl here (knowing that I need to compile with GraalVM)?

If so could you give an example in the README.md of how it would look like?

This is not to say that the README.md should cover all curl options of course, but it seems documenting this one could be handy.

Let me know if I can help. Thanks!

borkdude commented 4 years ago

@bzg Hi. You don't need to compile this library with GraalVM in order to use it You can also just use it from the JVM. Your example would look like:

(require '[babashka.curl :as curl])

(defn upload-to-datagouv []
  ;; Upload the csv
  (curl/post (str datagouv-api "/datasets/" dataset
                  "/resources/" resource-csv "/upload/")
             {:headers {"Accept" "application/json"
                        "X-Api-Key" datagouv-api-token}
              :raw-args ["-F" (str "file=@" csv-file-path)]}))

Can you explain what -F "file=@foo.csv" does? Does it send the entire csv file as a form parameter?

bzg commented 4 years ago

Sorry perhaps I was not clear: this is the whole program that I want to compile with GraalVM, not just the babashka library (I do so far, thus using clj-http-lite instead of clj-http e.g.).

About -F "file=@foo.csv", extracted from curl manpage:

 -F, --form <name=content>
          (HTTP SMTP IMAP) For HTTP protocol family, this lets curl emulate a filled-in
          form in which a user has pressed the submit button. This causes curl to POST
          data using the Content-Type multipart/form-data according to RFC 2388.

The @ construct is specific to this -F option. So it sends the file as Content-Type multipart/form-data. I am not sure how to deal with such request by reading the README.md right now.

borkdude commented 4 years ago

@bzg Ah, that makes sense. This library is compatible with GraalVM. There is no explicit support for sending files as form parameter, but we could certainly include that. Processing of form params is done here:

https://github.com/borkdude/babashka.curl/blob/3104d234a63fca71f0d3557c5d3dc23f4fbc294f/src/babashka/curl.clj#L89

We could check if the value is a file. If so, then we could transform that value into its path and append a "@" in front of it. PR + test for that is welcome.

bzg commented 4 years ago

Thanks, I will propose a PR.

borkdude commented 4 years ago

@bzg Fixed with https://github.com/borkdude/babashka.curl/commit/b6ad0648d8e59220924ea196e1ebac50cf6af503.

bzg commented 4 years ago

@borkdude Thanks a lot! And sorry for being MIA lately, I've been burried under a ton of job-related tasks.