请求主体方法PATCH覆盖给出错误“归因于丢失". [英] Request Body Method PATCH Override giving error "attributes missing"

查看:69
本文介绍了请求主体方法PATCH覆盖给出错误“归因于丢失".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过其API和Google Apps脚本更新名为3Commas的服务上的交易机器人.我正在尝试根据某些条件不时更新其使用的对(BTC_ETH,BTC_MANA等).此刻,我迷失了为什么呼叫中未读取有效负载信息的原因.

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.

最初,我尝试了一个失败的查询字符串.从阅读中可以看出,请求正文最适合POST/PUT操作.因此,我现在正在尝试使用请求正文.呼叫是通过PATCH进行的.从我所读的内容中,您需要对GAS中的PATCH使用POST方法和标头替代.我已经在"botParams"中包括了所有强制性参数.这是3commas文档:

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

感谢您的帮助.

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}


推荐答案

此修改如何?

  • 在您的脚本中,botParams用于查询参数和请求正文.
  • 使用botParams作为查询参数时,需要转换为查询参数.
  • 通过此'method': 'POST',,请求将成为POST方法.
  • 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.

在此修改后的脚本中,请求botParams作为查询参数.

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)

注意:

  • 此修改后的脚本假定editBotsbotParamskeysecret是正确的值.
  • 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?

      在此修改中,JSON.stringify()仅用于作为对象的pairsstrategy_list.这样,其他值不会被双引号引起来.

      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.

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

      收件人:

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

      尽管我不确定API的规格,您可以尝试这种修改吗? pairs=BTC_MANA&pairs=BTC_TRX&pairs=BTC_WAN.对于botParams,请不要从"pairs": ["BTC_MANA","BTC_TRX","BTC_WAN"],进行修改.

      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"],.

      为了对此进行测试,请按如下所示修改q +=...行.

      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 ? "&" : "");
      

      这篇关于请求主体方法PATCH覆盖给出错误“归因于丢失".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆