简体   繁体   中英

Google app api with freshdesk api error

On using the freshdesk api from google app script getting an error

"{"code":"invalid_content_type","message":"Content-Type header is set to . It should be set to application/json"}"

The code used for this

function hd_getTickets(){//using v2
    var API_KEY = 'xxxxxxxxxxxxxx';
    var headers = {'Content-type': 'application/json','Authorization': 'Basic ' + Utilities.base64Encode(API_KEY + ':X')    };
    var data = {  "query":"\"priority:3\"" };
    var ENDPOINT = 'https://xxxxxxx.freshdesk.com/api/v2'; 
    var url = ENDPOINT + '/search/tickets';  
    var options = {      'method': 'get', muteHttpExceptions: true,'headers': headers,'payload' : JSON.stringify(data)};
    var response = UrlFetchApp.fetch(url, options);
}

Changing the endpoint and removing the payload from options work so assuming authorization and header is fine

var url = ENDPOINT + '/tickets';  
var options = {'method':'get','headers':headers, muteHttpExceptions: true};

Using postman this works

https://xxxxxxx.freshdesk.com/api/v2/search/tickets?query="priority:3"

with header set as

Content-Type:application/json
Authorization:Basic xxxxxxxxxxxxxxxx

You are sending a GET request to the API with the Payload variable in the options. In my opinion payloads are used for POST requests.

Construct the URL with the query parameters and send it without the Payload. Example: ' https://domain.freshdesk.com/api/v2/search/tickets?query= "priority:3"'

See details here: HTTP GET with request body Freshdesk API Doc: https://developers.freshdesk.com/api/#filter_tickets

Two issues found 1) web site does not support payload based get. 2) google apps doesn't support special characters in url.

Adding parameters to the original url and encoding the double quotes works.

  var ENDPOINT = 'https://xxxxxx.freshdesk.com/api/v2';   
  var query ='query='+encodeURIComponent("\"priority")+":1"+encodeURIComponent("\"");
  var url = ENDPOINT + '/search/tickets?'+query;  
  var options = {'method': 'get', muteHttpExceptions: true,'headers': headers}; 
  var response = UrlFetchApp.fetch(url, options);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM