title: Strange boolean behavior in Clojure with checkbox in form-data
body: <p>I'm using an AngularJS resource to basically $.ajax() some form data. </p>
  
  <pre><code>Post.put({user:$scope.getLoggedInUser(), action:"new"},{body:$scope.postBody, "public":$scope.postPublic}, function(post) {
  </code></pre>
  
  <p>On the form is a checkbox named "public."</p>
  
  <p>I am using this function to merge the form data with the params from the URL:</p>
  
  <pre><code>(defn get-params [request]
    (merge (:params request) (parse-string (slurp (request :body)) true)))
  </code></pre>
  
  <p>when I println from my route's handler like so I get a nice <code>true</code> when the checkbox is checked:</p>
  
  <pre><code>(println (:public (get-params request)))
  </code></pre>
  
  <p>However, when I pass the parameter (i.e. not the println) to another function in my controller that talks to the database, I do another println at the beginning of that function and get <code>nil</code> instead.</p>
  
  <p>I've tried passing it as </p>
  
  <pre><code>(read-string x)
  (boolean (Boolean/valueOf x))
  (Boolean/valueOf x)
  </code></pre>
  
  <p>to no avail.</p>
  
  <p>One thing that might be causing it (but I don't know why) is that I'm wrapping the request through authentication like this: </p>
  
  <pre><code>(auth? request #(create x y z))
  </code></pre>
  
  <p>where the <code>(create)</code> function creates a record in the database.</p>
  
  <p>I can't get it to be <code>true</code> no matter what I've tried.</p>
  
  <p><em><strong>EDIT: more complete code</em></strong></p>
  
  <p><strong>CONTROLLER</strong></p>
  
  <pre><code>(defn auth? [request callback-true &amp; [callback-false]]
    (println (callback-true))
    (let [login-response (auth/login request)]
      (if (and (not (string? login-response))
               login-response)
        (callback-true)
        (if callback-false 
          (callback-false)
          (json-response (str "{\"auth\":" login-response "}"), 401)))))
  
  (defn create [user logged-in-user body public?]
    (if (= logged-in-user user)
      (json-response (post-view/single-post (post/create user body public?)))
      (json-response "{\"auth\":\"can't create post under different logged in user\"}" 401)))
  
  (defroutes routes
    ....
    (PUT "/api/:user/new" request
         (println request)
         (auth? request
                #(create (:user (request :params))
                         (:user (request :session))
                         (:body (get-params request))
                         (:public (get-params request)))))
    ....
  )
  </code></pre>
  
  <p><strong>MODEL</strong></p>
  
  <pre><code>(defn create [username body public?]
    (println public?)
    (when-not (and 
                (str/blank? body)
                (str/blank? username))
    (let [user-id (:id (get-id-from-username username))
          new-post-id
          (:id 
              (sql/with-connection db
                  (sql/insert-values :post
                [:usr_id :body :public] [user-id body (Boolean/valueOf public?)])))]
      (by-id new-post-id))))
  </code></pre>