How to send graphql request from usinf deluge script

19 Views Asked by At

I need to call the api from zoho crm , using custom function . The api is working fine on postman , but when I am calling it from zoho crm function it is giving error .

The Params calling from postman and it works fine :

{
  getLoanDetails(account:"399165061"){
   account
  }
}

my zoho crm function code is

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;

When I execute the function it is giving me error :

{"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}
1

There are 1 best solutions below

2
Shri Hari L On

The problem is with this line:

query = "{\"query\": \"{ getLoanDetails(account: \"399165061\") { account graceDays } }\", \"variables\": {} }";
info query;

We are assigning a JSON string to query variable with "s escaped leading it to a invalid JSON format.

If we print the above string, we get the following output:

{"query": "{ getLoanDetails(account: "399165061") { account graceDays } }", "variables": {} }

If we try to convert the above string as JSON, it will fail because of mismatched ".

enter image description here

We can make use of Deluge's in-built [Key Value data type] for assigning the query payload to solve this issue.

Corrected Code:

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;