cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

How to post json array to an external REST service?

zhuwenxi
11-Garnet

How to post json array to an external REST service?

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.
1 ACCEPTED SOLUTION

Accepted Solutions
slangley
23-Emerald II
(To:zhuwenxi)

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

View solution in original post

2 REPLIES 2
slangley
23-Emerald II
(To:zhuwenxi)

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

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!
Top Tags