title: How do I sign a POST request using HMAC-SHA512 and the Python requests library?
body: <p>I'm trying to use Python to access the trading API at poloniex.com, a cryptocurrency exchange. To do this I must follow this prescription:</p>
  
  <blockquote>
    <p>All calls to the trading API are sent via HTTP POST to <a href="https://poloniex.com/tradingApi" rel="noreferrer">https://poloniex.com/tradingApi</a> and must contain the following headers:</p>
    
    <p>Key - Your API key.<br>
    Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method.  </p>
    
    <p>Additionally, all queries must include a "nonce" POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used.</p>
  </blockquote>
  
  <p>Here is what I have so far. My current issue is that I do not know how to compile the POST url so that it can be signed without sending the incomplete request first. This obviously doesn't work.</p>
  
  <pre><code>import requests
  import hmac
  import hashlib
  import time
  
  headers = { 'nonce': '',
              'Key' : 'myKey',
              'Sign': '',}
  payload = { 'command': 'returnCompleteBalances',
              'account': 'all'}
  secret = 'mySecret'
  
  headers['nonce'] = int(time.time())
  response = requests.post( 'https://poloniex.com/tradingApi', params= payload, headers= headers )
  headers['Sign'] = hmac.new( secret, response.url, hashlib.sha512)
  </code></pre>