我需要使用自定义函数从 zoho crm 调用 api。该 API 在 postman 上工作正常,但是当我从 zoho crm 函数调用它时,它给出错误。
邮递员调用的参数,效果很好:
{
getLoanDetails(account:"399165061"){
account
}
}
我的 zoho crm 功能代码是
query = "{\"query\": \"{ getLoanDetails(account: \"399165061\") { account graceDays } }\", \"variables\": {} }";
header_data = Map();
header_data.put("Content-Type","application/json");
header_data.put("Authorization","Bearer key");
info query;
response = invokeurl
[
url :"https://fapi.myfci.com/graphql"
type :POST
parameters:query
headers:header_data
detailed:true
];
info response;
当我执行该函数时,它给了我错误:
{"responseText":{"errors":[{"message":"Expected a `String`-token, but found a `Integer`-token.","locations":[{"line":1,"column":39}],"extensions":{"code":"HC0011"}}]},"responseHeader":{"date":"Mon, 25 Mar 2024 09:54:23 GMT","transfer-encoding":"chunked","x-rate-limit-reset":"2024-03-25T09:54:38.8352528Z","content-type":"application/json; charset=utf-8","connection":"keep-alive","x-rate-limit-limit":"15s","x-rate-limit-remaining":"149","strict-transport-security":"max-age=15724800; includeSubDomains"},"responseCode":400}
问题出在这一行:
query = "{\"query\": \"{ getLoanDetails(account: \"399165061\") { account graceDays } }\", \"variables\": {} }";
info query;
我们将 JSON 字符串分配给
query
变量,其中 "
被转义,导致其变为无效的 JSON 格式。
如果我们打印上面的字符串,我们会得到以下输出:
{"query": "{ getLoanDetails(account: "399165061") { account graceDays } }", "variables": {} }
如果我们尝试将上面的字符串转换为 JSON,则会因为不匹配而失败
"
。
我们可以利用Deluge内置的[Key Value数据类型]来分配查询负载来解决这个问题。
更正代码:
query = {
"query": "{ getLoanDetails(account: \"399165061\") { account graceDays } }",
"variables": {}
};
info query;
header_data = Map();
header_data.put("Content-Type","application/json");
header_data.put("Authorization","Bearer key");
response = invokeurl
[
url :"https://fapi.myfci.com/graphql"
type :POST
parameters:query
headers:header_data
detailed:true
];
info response;