Giter VIP home page Giter VIP logo

Comments (25)

kkharji avatar kkharji commented on September 8, 2024

In think in curl command it can be written like this

curl --compressed --connect-timeout 5 -m 120 --retry 1 -fsSL --stderr - -H "Authorization: Client-ID ea6c0ef2987808e" -F "image=@\"/tmp/29-11-2020-03:05:14.png\"" https://api.imgur.com/3/image)"

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 I think slurp on an image is not a good idea as slurp will read the file as a string.

It's better to use (java.nio.file.Files/readAllBytes (.toPath (io/file "foo.png"))).

You can just send the file as the body maybe. In that case babashka.curl will use the -F option. See the README or tests how this is done.

Can you try if that works?

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

This test does it: https://github.com/babashka/babashka.curl/blob/master/test/babashka/curl_test.clj#L42-L46

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024
user=> (:command (curl/post "https://postman-echo.com/post" {:body (io/file "README.md") :debug true}))
["curl" "--silent" "--show-error" "--location" "--dump-header" "/var/folders/2m/h3cvrr1x4296p315vbk7m32c0000gp/T/babashka.curl11844491070583418627.headers" "--compressed" "--request" "POST" "-d" "@/Users/borkdude/Dropbox/dev/clojure/babashka/babashka.curl/README.md" "https://postman-echo.com/post"]

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

It's better to use (java.nio.file.Files/readAllBytes (.toPath (io/file "foo.png"))).

Thanks for this useful method @borkdude

Same issue it just return 400 err, I tried command before but it doesn't return but 400 err

(def error
  (:command 
   (curl/post "https://api.imgur.com/3/image"
              {:headers {"Content-Type" "image/png"
                         "Authorization" "Client-ID ea6c0ef2987808e"
                         "Connection" "keep-alive"}
               :body (java.nio.file.Files/readAllBytes (.toPath (io/file "/tmp/29-11-2020-03:05:14.png"))) 
               :debug true})))

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

Ah, I see. Sending bytes via the command line might not work correctly.

@tami5 What about:

(curl/post "https://postman-echo.com/post" {:body (io/file "README.md") :debug true})

but then adapted to your imgur example?

Can you post the exact output from that (without :proc)?

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Just used (.encodeToString (Base64/getEncoder) (java.nio.file.Files/readAllBytes (.toPath (io/file "/tmp/29-11-2020-03:05:14.png")))) because server expects base64 encoding

Ohh got it

(def error
  (:body
   (curl/post "https://api.imgur.com/3/image"
              {:headers {"Content-Type" "image/png"
                         "Authorization" "Client-ID ea6c0ef2987808e"
                         "Connection" "keep-alive"}
               :body (.encodeToString (Base64/getEncoder) (java.nio.file.Files/readAllBytes (.toPath (io/file "/tmp/29-11-2020-03:05:14.png")))) 
               :debug true})))

By the way with :proc it just returns 400 and without the same

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 Is this a valid client id which I can try?

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Yes, its valid Its kinda anonymous id :smile, personally got it from a open source project

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 I just posted an image using that client ID and got 200 back. Where can I see the image?

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

parse the json body, and it should be in :link

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

sorry in :data {:link string}

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 Seems to have worked:

https://i.imgur.com/Q5aKJfn.png

icon

Script:

(require '[babashka.curl :as curl]
         '[cheshire.core :as json]
         '[clojure.java.io :as io])

(def response
  (curl/post "https://api.imgur.com/3/image"
             {:headers {"Content-Type" "image/png"
                        "Authorization" "Client-ID ea6c0ef2987808e"
                        "Connection" "keep-alive"}
              :body (.encodeToString (java.util.Base64/getEncoder)
                                     (java.nio.file.Files/readAllBytes
                                      (.toPath (io/file "/Users/borkdude/Dropbox/dev/clojure/babashka/logo/icon.png"))))}))

(json/parse-string (:body response))

Output:

{"data" {"in_most_viral" false, "width" 128, "is_ad" false, "height" 128, "bandwidth" 0, "ad_type" 0, "tags" [], "id" "UpPfHOM", "ad_url" "", "in_gallery" false, "name" "", "account_url" nil, "has_sound" false, "section" nil, "vote" nil, "views" 0, "nsfw" nil, "animated" false, "datetime" 1606668032, "title" nil, "link" "https://i.imgur.com/UpPfHOM.png", "type" "image/png", "deletehash" "7Z3FSPsqdstOY2T", "size" 7748, "account_id" 0, "favorite" false, "edited" "0", "description" nil}, "success" true, "status" 200}

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Yep it works now for me as well, but why can't we debug it if there ware an error like that. :command or anything else I tried doesn't let me inspect and see what went wrong.

BTW thanks a lot for your time

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 This is how I would have debugged it:

(require '[babashka.curl :as curl]
         '[cheshire.core :as json])

(def resp (curl/post "https://api.imgur.com/3/image"
                     {:headers {"Content-Type" "image/png"
                                "Authorization" "Client-ID ea6c0ef2987808e"
                                "Connection" "keep-alive"}
                      :body "foo"
                      :throw false}))

(prn (json/parse-string (:body resp)))

Output:

{"data" {"error" "Invalid URL (foo)", "request" "/3/image", "method" "POST"}, "success" false, "status" 400}

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 I now see that you used the option :as :stream. This will return the body as an inputstream. I'm not sure why you did this, but this is why you didn't see the error message like in the example above.

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

@tami5 I now see that you used the option :as :stream. This will return the body as an inputstream. I'm not sure why you did this, but this is why you didn't see the error message like in the example above.

:D I was trying stuff out to find the issue. I deeply appreciate those awesome insights @borkdude . thanks a lot

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Hey @borkdude Not sure if this issue is related here, but when I try to upload full screen image (exceeding 41kb) I get back error=7, Argument list too long (using #31 (comment))

Execution error (IOException) at java.lang.ProcessImpl/forkAndExec (ProcessImpl.java:-2).
error=7, Argument list too long

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 This is a limitation which comes with using curl and command line arguments. What you could do is spit out the base64 string to a file and then use (io/file ...) as the body. Can you try this?

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Sure. one sec

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Yep this worked for me

(def response
  (curl/post "https://api.imgur.com/3/image"
             {:headers {"Content-Type" "image/png"
                        "Authorization" "Client-ID ea6c0ef2987808e"
                        "Connection" "keep-alive"}
              :body (do (->> (.encodeToString
                               (java.util.Base64/getEncoder)
                               (-> "/tmp/2020-11-30_01:12.png"
                                   io/file
                                   .toPath
                                   java.nio.file.Files/readAllBytes))
                             (spit "/tmp/upload"))
                        (io/file "/tmp/upload"))}))

(:link (:data (json/parse-string (:body response) true)))

Thanks again @borkdude , also don't you think that might be there is better way for curl to work regardless ?

I seen some people solve this issue for find related via piping

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

@tami5 Can you repeat your comments after the code? I can't understand them.

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Oh I meant to say that it would be better if babashka.curl would handle this error and work regardless of argument list being too long. For example

echo */*/*/*/*.jpg | xargs ls

is a work around for ls */*/*/*/*.jpg not working using pipes

from babashka.curl.

borkdude avatar borkdude commented on September 8, 2024

I'll consider it.

from babashka.curl.

kkharji avatar kkharji commented on September 8, 2024

Thanks

from babashka.curl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.