Petr Sedláček
Petr Sedláček

Reputation: 25

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

Upvotes: 1

Views: 244

Answers (3)

Petr Sedl&#225;ček
Petr Sedl&#225;ček

Reputation: 25

I can't find any difference but this actually started working. I believe the problem was caused by combination of mistakes. I removed one problem but perhaps add another one. Here is a code which works.

function createPost(objId, post) {
  objId = '313878829908';
  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, payload));
  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;

  var options={
    'headers': {'ZSESSIONID': rallyApiKey },
    'payload': JSON.stringify(payload),
    'method': method,
    'contentType':'application/json'
  };
  return options;
}

Thanks Tanaike for your help. I really appreciate it.

Upvotes: 0

Petr Sedl&#225;ček
Petr Sedl&#225;ček

Reputation: 25

Thanks for the fast response. With the following

var payload = {"ConversationPost":{"Artifact": "/portfolioitem/capability/"+objId,"Text": post}};
  var method = "POST";
  var options = optionsPost_(method, payload);

and

var options={
    headers: { "ZSESSIONID": rallyApiKey },
    payload: payload,
    method: method,
    contentType:"application/json"
  };

It gives me

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

If I change it to

var options={
        headers: { "ZSESSIONID": rallyApiKey },
        payload: JSON.stringify(payload),
        method: method,
        contentType:"application/json"
      };

It gives me

Cannot parse input stream due to I/O error as JSON document: Parse error: expected '}' but saw ',' [ chars read = >>>{"headers":{"ZSESSIONID":"_ycHaCSd2QZSf8kbkQ0R1yhjohUvSzUYas0caApHt2A"},<<< ]

Only documentation I use is this: https://rally1.rallydev.com/slm/doc/webservice/objectModel.sp#ConversationPost

Upvotes: 0

Tanaike
Tanaike

Reputation: 201513

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:

Upvotes: 1

Related Questions