Search code examples
apigoogle-apps-scriptpostrallypayload

Rally API create ConversationPost


with the following code I'm trying to create new conversation post for Capability. But it says

Cannot parse input stream due to I/O error as JSON document: Parse error: expected '{' but saw '￿' [ chars read = >>>￿<<< ]

function createPost(objId, post) {
  objId = "313878829904";
  post = "<p>MindMap:Hello from GAS.</p>"
  var url = "https://rally1.rallydev.com/slm/webservice/v2.0/conversationpost/create";
  
  var payload = {
    "ConversationPost": {
      "Artifact": "/portfolioitem/capability/" + objId,
      "Text": post
    }
  }
  
  var method = "POST";
  var options = optionsPost_(method, payload);
  var response = UrlFetchApp.fetch(url, optionsPost_(method, options));
  var content = JSON.parse(response.getContentText());
  content.CreateResult.Errors.forEach(error => Logger.log(error));
}

function optionsPost_(method, payload) {
  var rallyApiKey = "";
  if (rallyApiKey != "") {
    PropertiesService.getScriptProperties().setProperty("RallyApiKey", rallyApiKey);
  } else {
    rallyApiKey = PropertiesService.getScriptProperties().getProperty("RallyApiKey");
  }

  if (rallyApiKey == null) return null;

  return {
    headers: { "ZSESSIONID": rallyApiKey },
    payload: payload,
    method: method
  };
}

I can't spot any problem. Could you please help? Thank you! Petr


Solution

  • I thought that from your error message, the payload might be required to be sent as JSON data. If my guessing is correct, how about the following modification?

    Modified script:

    From:
    return {
      headers: { "ZSESSIONID": rallyApiKey },
      payload: payload,
      method: method
    };
    
    To:
    return {
      headers: { "ZSESSIONID": rallyApiKey },
      payload: JSON.stringify(payload),
      method: method,
      contentType: "application/json"
    };
    

    Note:

    • In this modification, it supposes that the values of payload and rallyApiKey are valid values for using the API. Please be careful this.
    • When above modification was not the dierct solution of your issue, can you provide the official document of API you want to use? By this, I would like to confirm it.

    Reference: