Comments (25)
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.
@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.
This test does it: https://github.com/babashka/babashka.curl/blob/master/test/babashka/curl_test.clj#L42-L46
from babashka.curl.
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.
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.
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.
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.
@tami5 Is this a valid client id which I can try?
from babashka.curl.
Yes, its valid Its kinda anonymous id :smile, personally got it from a open source project
from babashka.curl.
@tami5 I just posted an image using that client ID and got 200 back. Where can I see the image?
from babashka.curl.
parse the json body, and it should be in :link
from babashka.curl.
sorry in :data {:link string}
from babashka.curl.
@tami5 Seems to have worked:
https://i.imgur.com/Q5aKJfn.png
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.
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.
@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.
@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.
@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.
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.
@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.
Sure. one sec
from babashka.curl.
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.
@tami5 Can you repeat your comments after the code? I can't understand them.
from babashka.curl.
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.
I'll consider it.
from babashka.curl.
Thanks
from babashka.curl.
Related Issues (20)
- curl can print multiple headers after HTTP 100 Continue
- Use -D <tempfile> to write headers to tempfile and parse them from there, instead of stdout
- Capture standard error from curl shell command HOT 8
- Resource leak HOT 10
- Add documentation for data-raw HOT 5
- form-params are sent as multipart/form-data HOT 2
- Support :multipart HOT 7
- Support (S)FTP
- Pass --compressed by default.
- Segmentation fault when reading /etc/passwd HOT 23
- Add to clojars HOT 6
- Is there a way to display curl progress information? HOT 1
- As well as --data-raw, add --data-binary HOT 8
- Bundled curl with Windows 10 doesn't support `--compressed` HOT 4
- Cannot retry when using `:as :stream` option HOT 13
- Make redirect (--location) optional
- Last set-cookie headers on a page overwrites the ones before HOT 11
- Set up CI HOT 1
- BREAKING: Always parse response headers and return map
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babashka.curl.