Request Body Method PATCH Override giving error “a

2019-06-14 12:06发布

I'm attempting to update a trading bot on a service called 3Commas via their API and Google Apps Script. I'm trying to update the pairs (BTC_ETH, BTC_MANA etc) it uses from time to time based on certain conditions. At the moment I'm lost on why the payload information isn't being read within the call.

Initially I tried a query string which failed. From reading, I see the request body is best for POST/PUT actions. So I am now trying it with a request body. The call is via PATCH. From what I have read, you need to use the POST method and a header override for PATCH in GAS. I've included all mandatory params in "botParams". Here is the 3commas documentation: 3commas

Thanks for any help.

try {
  var editBots = "/ver1/bots/250549/update";

  var baseUrl = "https://3commas.io";
  var endPoint = "/public/api"+editBots+"?";


  var botParams = {
    "name": "cqstoshi",
    "pairs": ["BTC_MANA","BTC_TRX","BTC_WAN"],
    "base_order_volume": 0.001,
    "take_profit": 1.5,
    "safety_order_volume": 0.001,
    "martingale_volume_coefficient": 2,
    "martingale_step_coefficient": 1,
    "max_safety_orders": 2,
    "active_safety_orders_count": 1,
    "safety_order_step_percentage": 2.5,
    "take_profit_type": "total",
    "strategy_list": [{"strategy":"cqs_telegram"}],
    "bot_id": 250549
    };

  var payload = JSON.stringify(botParams)


  var totalParams = endPoint + payload; 
  Logger.log(totalParams)
  var signature = Utilities.computeHmacSha256Signature(totalParams, secret);
  signature = signature.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");


  //headers
  var headers = {
    'APIKEY': key,
    'Signature': signature,
    "X-HTTP-Method-Override": "PATCH"
    };

   var params = {
    'method': 'POST',
    'headers': headers,
    'payload' : payload,

    muteHttpExceptions: true
  };
  //call
  var data = UrlFetchApp.fetch(baseUrl + endPoint, params).getContentText();
  var json = JSON.parse(data);  
    Logger.log(json)
  } catch (err) {Logger.log(err)}

//This is a logger report and the error I am currently receiving:


//Logger
[19-01-24 15:00:45:304 EST] 
/public/api/ver1/bots/250549/update? 
{"name":"cqstoshi",
"pairs":["BTC_MANA","BTC_TRX","BTC_WAN"],
"base_order_volume":0.001,
"take_profit":1.5,
"safety_order_volume":0.001,
"martingale_volume_coefficient":2,
"martingale_step_coefficient":1,
"max_safety_orders":2,
"active_safety_orders_count":1,
"safety_order_step_percentage":2.5,
"take_profit_type":"total",
"strategy_list":[{"strategy":"cqs_telegram"}],"bot_id":250549}

//Error
[19-01-24 15:00:45:608 EST]
{error_attributes={base_order_volume=[is missing],
safety_order_volume=[is missing], 
martingale_volume_coefficient=[is missing], 
strategy_list=[is missing], 
take_profit=[is missing], 
max_safety_orders=[is missing], 
martingale_step_coefficient=[is missing], 
active_safety_orders_count=[is missing], 
name=[is missing], 
take_profit_type=[is missing, does not have a valid value], 
safety_order_step_percentage=[is missing], 
pairs=[is missing]}, 
error_description=Invalid parameters, 
error=record_invalid}


1条回答
2楼-- · 2019-06-14 12:58

How about this modification?

Modification points:

  • In your script, botParams is used for both the query parameters and the request body.
  • When botParams is used as the query parameters, it is required to convert to the query parameters.
  • By this 'method': 'POST',, the request becomes the POST method.

When above points are reflected to your script, it becomes as follows.

Modified script:

In this modified script, botParams is requested as the query parameter.

var editBots = "/ver1/bots/250549/update";
var baseUrl = "https://3commas.io";
var endPoint = "/public/api"+editBots+"?";
var botParams = {
  "name": "cqstoshi",
  "pairs": ["BTC_MANA","BTC_TRX","BTC_WAN"],
  "base_order_volume": 0.001,
  "take_profit": 1.5,
  "safety_order_volume": 0.001,
  "martingale_volume_coefficient": 2,
  "martingale_step_coefficient": 1,
  "max_safety_orders": 2,
  "active_safety_orders_count": 1,
  "safety_order_step_percentage": 2.5,
  "take_profit_type": "total",
  "strategy_list": [{"strategy":"cqs_telegram"}],
  "bot_id": 250549
};
var keys = Object.keys(botParams); // Added
var totalParams = keys.reduce(function(q, e, i) { // Added
  q += e + "=" + encodeURIComponent(JSON.stringify(botParams[e])) + (i != keys.length - 1 ? "&" : ""); // Modified
  return q;
}, endPoint);
Logger.log(totalParams)
var signature = Utilities.computeHmacSha256Signature(totalParams, secret);
signature = signature.map(function(e) {return ("0" + (e < 0 ? e + 256 : e).toString(16)).slice(-2)}).join("");
var headers = { // Modified
  'APIKEY': key,
  'Signature': signature,
};
var params = { // Modified
  'method': 'PATCH',
  'headers': headers,
  muteHttpExceptions: true
};
var data = UrlFetchApp.fetch(baseUrl + totalParams, params).getContentText(); // Modified
var json = JSON.parse(data);
Logger.log(json)

Note:

  • This modified script supposes that editBots, botParams, key and secret are the correct values.

I cannot test this. So when this didn't work, I apologize. At that time, can you provide the detail information of the response values?

Edit 1:

In this modification, JSON.stringify() is used for only pairs and strategy_list which are an object. By this, other values are not enclosed by the double quotes.

From:

q += e + "=" + encodeURIComponent(JSON.stringify(botParams[e])) + (i != keys.length - 1 ? "&" : ""); // Modified

To:

q += e + "=" + (typeof botParams[e] == "object" ? encodeURIComponent(JSON.stringify(botParams[e])) : encodeURIComponent(botParams[e])) + (i != keys.length - 1 ? "&" : "");

Edit 2:

Although I'm not sure about the specification of the API, can you try this modification? It's pairs=BTC_MANA&pairs=BTC_TRX&pairs=BTC_WAN. For botParams, please don't modify from "pairs": ["BTC_MANA","BTC_TRX","BTC_WAN"],.

In order to test this, please modify the line of q +=... as follows.

q += (e == "pairs" ? botParams[e].reduce(function(s, f, j) {
  s += e + "=" + f + (j != botParams[e].length - 1 ? "&" : "");
  return s;
},"") : e + "=" + (typeof botParams[e] == "object" ? encodeURIComponent(JSON.stringify(botParams[e])) : encodeURIComponent(botParams[e]))) + (i != keys.length - 1 ? "&" : "");
查看更多
登录 后发表回答