urllib2.Request post等效于node.js

时间:2014-02-09 22:11:49

标签: python node.js request urllib2

我正在尝试将coin-e API与node.js应用程序一起使用,但我遇到了经过身份验证的POST请求的问题。我在这里用python写了一个请求:

post_args['method'] = method        
post_args['nonce'] = nonce        
post_data = urllib.urlencode(post_args)
required_sign = hmac.new(PRIVATE_KEY, post_data, hashlib.sha512).hexdigest()
headers = {}
headers['key'] = PUBLIC_KEY
headers['sign'] = required_sign
url_request_object = urllib2.Request("%s/%s" % (BASE_API_URL,url_suffix),
                                 post_data,
                                 headers)    
response = urllib2.urlopen(url_request_object)    


try:
    response_content = response.read()
    response_json = json.loads(response_content)
    print response_json
finally:
    response.close()

这是我写的尝试做同样事情的node.js代码:

var request = require('request');
var querystring = require('querystring');
var crypto = require('crypto');

var post_args = {};
post_args['nonce'] = nonce;
post_args['method'] = method;
var post_data = querystring.stringify(postdata);

var hasher = crypto.createHmac('sha512', PRIVATE_KEY);
hasher.update(post_data);
var required_sign = hasher.digest('hex');

var headers = {};

headers['key'] = PUBLIC_KEY;
headers['sign'] = required_sign;

var options = {
    uri: url,
    qs: post_data,
    method: 'POST',
    headers: headers
};

request(options, function(error, response, body){
    console.log(body);
});

想看看是否有人可以告诉我在python中创建的帖子与node.js中的帖子之间有什么区别,或者是否有人可以帮我设置有效的coin-e API的发布请求的node.js。

以下是coins-e API的链接:https://www.coins-e.com/exchange/api/documentation/ 有关如何格式化经过身份验证的请求的说明大约是在第6项之前的页面的一半。感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我在那里没有帐号来测试它,但它看起来应该是

var post_data = querystring.stringify(post_args);