Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replicating curl command in babaska

I have the following curl command from the github api that I wish to replicate in babashka:

curl \
  -X POST \
  -H "Accept: application/json" \
  -H "Authorization: Bearer <YOUR-TOKEN>" \
  https://api.github.com/user/repos \
  -d '{"name":"test"}'

Currently, I have tried:

(ns bb-test
 (require [environ.core :refer [env]]
          [babashka.curl :as curl])

(def url "https://api.github.com/user/repos")
(def token (env :token))
(def headers {"Accept" "application/json" "Authorization" (str "Bearer " token)})
(def body "{'name:' 'test'}")

(curl/post url {:headers headers :body body})

I get back a 400 http status code. Any help appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In the bash example, you’re sending a body with {"name":"test"} as the literal data sent over the wire to the server.

With the babashka example, you’re sending a body with {'name:' 'test'} as the literal data sent over the wire to the server.

Only one of these things is valid JSON.

The Clojure syntax to describe a literal string containing data {"name":"test"} is "{\"name\":\"test\"}", so the smallest change is to modify the line where you run def body:

(def body "{\"name\":\"test\"}")

Alternately, you can use a JSON library such as Cheshire to encode a native Clojure structure into a JSON string:

(ns bb-test
 (require [environ.core :refer [env]]
          [cheshire.core :as json]
          [babashka.curl :as curl])

(def url "https://api.github.com/user/repos")
(def token (env :token))
(def headers {"Accept" "application/json"
              "Authorization" (str "Bearer " token)})
(def body {"name" "test"})

(curl/post url {:headers headers
                :body (json/generate-string body)})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading