How to send the body in http request (CURL way)?

@camdez Thanks for the solution in the post how-to-send-http-requests-the-curl-way-or-help-with-http-clj

I am trying to send the data in the JSON body instead of query parameters.
It would be great if anyone help me out to achieve it.

(ns foo
  (:require [clj-http.client :as client]
            [clj-http.headers :as headers]))

(defn request [req]
  (client/with-middleware [headers/wrap-header-map
                           client/wrap-query-params
                           client/wrap-url
                           client/wrap-output-coercion
                           client/wrap-method]
    (client/request req)))

;; `:keys` here are just included for documentation
(defn curl [method url & {:keys [headers query-params] :as req}]
  (request (merge req {:method method :url url})))

;; curl -X POST --header "Content-Type: application/json" -d "name=Bob" "http://google.com"
(curl :post "http://google.com" :headers {"Content-Type" "application/json"} :query-params {"name" "Bob"})

Have you tried :body the-json-string?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.