user3586358
user3586358

Reputation: 99

Google Script - javascript function to send JSON POST request

For a spreadsheet Im building in google drive, I want to pull my balances off a trading website. I have a function which works with the JSON GET method, but now I need to find a function that works for JSON POST data. The function should use 3 parameters, so I can select those on the spreadsheet:

  1. url.
  2. key (API key)
  3. sign (secret)

These are the headers to send with the function!

The site (poloniex.com/api) has the following requirements for a request:

All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers:

Key - Your API key.
Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method.
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.

So, can anyone provide me with a working version of .gs or point me to a good source. Also, I have no clue how to write the code for a nonce.

Upvotes: 1

Views: 959

Answers (1)

Eric Koleda
Eric Koleda

Reputation: 12673

The documentation for UrlFetchApp.fetch() describes the optional parameters you can use. Set the parameter method to "post" to send a POST request instead of a GET.

You can use Utilities.computeHmacSignature() to sign the request, although figuring that out will likely be tricky.

As for a nonce, it's probably enough to use the current timestamp, available from new Date().getTime().

Upvotes: 1

Related Questions