clj-http client translated from php/curl implementation fails, why?

155 views
Skip to first unread message

Joachim De Beule

unread,
Feb 13, 2014, 7:14:31 AM2/13/14
to clo...@googlegroups.com
Dear list,

[Sorry if this may be a bit of topic, but I don't know much about php and http, and I'm not sure where else to turn]

I'm trying to port a working php api client implementation to clojure.

So on the one hand, I've got the following working php code for performing a signed post request:

function perform_request($url, $method, $key, $secret) {
        $query_params['method'] = $method;
        $mt = explode(' ', microtime());
        $query_params['nonce'] = $mt[1]*1000;
        $query_string = http_build_query($query_params, '', '&');
        $sign = hash_hmac("sha512", $query_string, $secret);
        $headers = array(
                'sign: '.$sign,
                'key: '.$key,
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $res = curl_exec($ch);
        return $res;
}


On the other hand, I've got the following clojure code which, as far as I can tell, performs an identical request (I've checked that the signatures are the same in both implementations):

(defn perform-request [url method key secret]
  (let [query-params {:method method
                                    :nonce (clj-time.coerce/to-long (clj-time.core/now))}
         query-string (clj-http.client/generate-query-string query-params)
         signature (pandect.core/sha512-hmac query-string secret)]
    (clj-http.client/post url
                                    {:query-params query-params
                                     :headers {"sign" signature
                                                      "key" key}})))

Unfortunately, the clojure code always returns with an "unable to authorize request, check post data" response from the remote service, and I don't understand why? Is it possible that the default client settings in php/curl are different from those in clj-http or something, i.e. maybe I need to disable/add some clj-http request wrappers or something? 

Gijs S.

unread,
Feb 13, 2014, 7:35:46 AM2/13/14
to clo...@googlegroups.com

Untested of course, but I think you need to use either :body or :form-params rather than :query-params to put the query-params data into the body of the post request.

Kevin Ilchmann Jørgensen

unread,
Feb 13, 2014, 8:03:18 AM2/13/14
to clo...@googlegroups.com
They return identical data if you use http://httpbin.org/post as url ?

/Kevin


On Thu, Feb 13, 2014 at 1:35 PM, Gijs S. <gijsst...@gmail.com> wrote:

Untested of course, but I think you need to use either :body or :form-params rather than :query-params to put the query-params data into the body of the post request.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Joachim De Beule

unread,
Feb 13, 2014, 8:08:33 AM2/13/14
to clo...@googlegroups.com
That did the trick! Thanks!!

Op donderdag 13 februari 2014 13:35:46 UTC+1 schreef Gijs S.:

Joachim De Beule

unread,
Feb 13, 2014, 8:12:34 AM2/13/14
to clo...@googlegroups.com
Solved by Gijs S., but thanks anyway, I didn't know about httpbin.org.

Op donderdag 13 februari 2014 14:03:18 UTC+1 schreef Kevin Ilchmann Jørgensen:
Reply all
Reply to author
Forward
0 new messages