 (defn managed-form-post [url & {:keys [params headers keywords?]
                                 :or {keywords? true}}]
   (let [channel (chan)]
-    (clj-ajax/ajax-request url :post
-                           (clj-ajax/transform-opts
-                            {:format (merge (clj-ajax/url-request-format)
-                                            (json-response-format {:keywords? keywords? :url url :method :post}))
-                             :response-format :json
-                             :params params
-                             :headers (merge {:Accept "application/json"}
-                                             (when (re-find #"^/" url)
-                                               {:X-CSRFToken (utils/csrf-token)})
-                                             headers)
-                             :handler #(put! channel (assoc % :status :success))
-                             :error-handler #(put! channel %)
-                             :finally #(close! channel)}))
+    (-> (clj-ajax/transform-opts
+         {:format (merge (clj-ajax/url-request-format)
+                         (json-response-format {:keywords? keywords? :url url :method :post}))
+          :response-format :json
+          :params params
+          :headers (merge {:Accept "application/json"}
+                          (when (re-find #"^/" url)
+                            {:X-CSRFToken (utils/csrf-token)})
+                          headers)
+          :handler #(put! channel (assoc % :status :success))
+          :error-handler #(put! channel %)
+          :finally #(close! channel)})
+        (assoc :uri url :method :post))
     channel))

                        (if prefix (str " prefix '" prefix "'"))
                        (if keywords? " keywordize"))}))

(defn xml-request-response []
  {:read (fn read-xml [xhrio]
           (set! js/window.testx (.getResponseXml xhrio))
           {:resp (.getResponseXml xhrio)})
   :description "XML"
   :content-type "application/xml"
   :write identity})

(defn scopes-from-response [api-resp]
  (if-let [scope-str (get-in api-resp [:response-headers "X-Circleci-Scopes"])]

                                       format :json
                                       csrf-token true}}]
  (let [channel (chan)
        accept (case format
                 :json "application/json"
                 :xml "applicatin/xml")]
    (-> (clj-ajax/transform-opts {:format (case format
                                            :json (clj-ajax/json-request-format)
                                            :xml (xml-request-format))
                                  :response-format (case format
                                                     :json (json-response-format {:keywords? keywords? :url url :method method})
                                                     :xml (xml-response-format))
                                  :keywords? keywords?
                                  :params params
                                  :headers (merge {:Accept accept}
                                                  (when (and csrf-token (re-find #"^/" url))
                                                    {:X-CSRFToken (utils/csrf-token)})
                                                  headers)
                                  :handler #(put! channel (assoc % :status :success :scopes (scopes-from-response %)))
                                  ;; TODO: clean this up
                                  :error-handler #(put! channel (normalize-error-response % {:url url}))
                                  :finally #(close! channel)})
        (assoc :uri url :method method)
        clj-ajax/ajax-request)
    channel))


