Skip to main content
12-Amethyst
November 16, 2018
Solved

How to post json array to an external REST service?

  • November 16, 2018
  • 1 reply
  • 4920 views
I write a TWX script to send http post to an external REST service as below.
var headers = {
"content-type": "application/json;charset=utf-8"
};
var queryParams = [
{
"ColumnName": "TaskGroupID",
"Sign": "=",
"Value": "c0c3f361-b8f0-4d98-8ab3-f507179beb79"
},
{
"ColumnName": "TaskItemID",
"Sign": "=",
"Value": "'d24ba69e-82be-49d4-abbe-3821c371bae5','10bc58c0-0583-4cf4-b7fb-98ad5e99eafa','45c74c31-dace-48c4-9784-5157b001090b','b41ce652-c325-4e03-8dfe-d2693ced01d4'"
}
];
 
var params = {
    headers: headers /* JSON */,
    ignoreSSLErrors: true /* BOOLEAN */,       
    url: "http://localhost:8010/api/Report/GetTaskItemReport?isCurrentReport=false" /* STRING */,
    content: queryParams /* JSON */,
    timeout: 60000 /* NUMBER */,
    password: me.Password /* STRING */,
    username: me.UserName /* STRING */
};
var result = Resources["ContentLoaderFunctions"].PostJSON(params);
 
But the REST server receive the content like 
{"array":[
{
"ColumnName": "TaskGroupID",
"Sign": "=",
"Value": "c0c3f361-b8f0-4d98-8ab3-f507179beb79"
},
{
"ColumnName": "TaskItemID",
"Sign": "=",
"Value": "'d24ba69e-82be-49d4-abbe-3821c371bae5','10bc58c0-0583-4cf4-b7fb-98ad5e99eafa','45c74c31-dace-48c4-9784-5157b001090b','b41ce652-c325-4e03-8dfe-d2693ced01d4'"
}
]}
 
The PostJSON api send my json array as a json object with key 'array'. That make the REST server no working properly.
Best answer by slangley

Hi @zhuwenxi.

 

Are you able to use another tool such as Postman to post on your REST service?  If it still fails to post correctly, that would indicate there is a problem with the syntax.  Another option is to change your TWX script to use PostTEXT, while keeping the rest of the content the same.

 

Please test these out and let us know the results.

 

Regards.

 

--Sharon

1 reply

slangleyCommunity ManagerAnswer
Community Manager
December 3, 2018

Hi @zhuwenxi.

 

Are you able to use another tool such as Postman to post on your REST service?  If it still fails to post correctly, that would indicate there is a problem with the syntax.  Another option is to change your TWX script to use PostTEXT, while keeping the rest of the content the same.

 

Please test these out and let us know the results.

 

Regards.

 

--Sharon

zhuwenxi12-AmethystAuthor
12-Amethyst
December 4, 2018
PostTEXT works and got the correct response. But the response is a string and will attached the header at the end of the result string. I need to get rid of the header and parse the string as json. Thanks!