使用Post方法为Rest Web服务设置setRequestBody

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Post方法为Rest Web服务设置setRequestBody相关的知识,希望对你有一定的参考价值。

嗨我正在尝试使用post方法创建一个休息响应,我想动态传递变量而不是硬编码,但我失败的地方是,当我试图将一个数组作为参数发送到Rest Web服务使用post方法(示例数组[“CN = XXX_XX,OU = XXXXX,OU = 1_XXXX XXXXity组,DC = XXXX,DC =本地”])我知道有更好的方法可以找到我的代码示例。这是给我一个合适结果的方法。第一种方法:(作品)`

 try {
          var r = new sn_ws.RESTMessageV2('SailPoint_IdM', 'post');
          var txt = "{
	"workflowArgs":
	{
	"identityName":"SiamR",
	"appName":"Active Directory",
	"listEntitlements":["CN=ER_CxxxK,OU=xxxxx,OU=1_xxxxxx Security xxx,DC=xxxx,DC=local"],
	"operation":"Add",
	"ticketNumber":"RITM1234567"
	}
}";
    r.setRequestBody(txt);
    var response = r.execute();
    var ResponseBody = response.getBody();
    var HTTPCode = response.getStatusCode();
    gs.log(ResponseBody);
    gs.log(HTTPCode);

} catch (ex) {
    var message = ex.getMessage();
}

输出:

 Script: {"attributes":{"requestResult":{"status":"Success"}},"complete":false,"errors":null,"failure":false,"metaData":null,"requestID":"2c988d8c5bd47cf7015bebfb64cf01e6","retry":false,"retryWait":0,"status":null,"success":false,"warnings":null}
 Script: 200

2n方法(不起作用):

try {
 var r = new sn_ws.RESTMessageV2('SailPoint_IdM', 'post');
 r.setStringParameter('"listEntitlements"', '["CN=Exxx_xxxK,OU=xxxxion,OU=1_xxxxx Security xxxx,DC=xxx,DC=xxxx"]');
 r.setStringParameter('"identityName"', '"SiarmR"');
 r.setStringParameter('"appName"', '"Active Directory"');
 r.setStringParameter('"ticketNumber"', '"RITM1234567"');
 r.setStringParameter('operation', '"Add"');

//override authentication profile 
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);

var response = r.execute();
 var responseBody = response.getBody();
 var httpStatus = response.getStatusCode();
gs.log(responseBody );
}
catch(ex) {
 var message = ex.getMessage();
}

输出:

 Script: {"attributes":{"requestResult":{"errors":["An unexpected error occurred: sailpoint.tools.GeneralException: The application script threw an exception: java.lang.NullPointerException: Null Pointer in Method Invocation BSF info: script at line: 0 column: columnNo"],"status":"FAIL","GroupStatus":null,"AppStatus":null}},"complete":false,"errors":["Status : failed
An unexpected error occurred: sailpoint.tools.GeneralException: The application script threw an exception: java.lang.NullPointerException: Null Pointer in Method Invocation BSF info: script at line: 0 column: columnNo
"],"failure":false,"metaData":null,"requestID":null,"retry":false,"retryWait":0,"status":null,"success":false,"warnings":null}

 Script: 200

我正面临着这个参数的问题,因为我试图将其传递为数组参数'qazxsw poi'

请提出一种实现方法,并在建议第一种方法时动态传递所有变量

答案

下面是我的一个函数,用于处理请求端点(url),头文件或正文中出现的动态参数;

例如:参数p

["CN=Exxx_xxxK,OU=xxxxion,OU=1_xxxxx Security xxxx,DC=xxx,DC=xxxx"]

和outbuond休息设置:

var p = {abc: 'def'}; 

所以它会将所有$ {abc}替换为'def'

rest url =  https://xxxx.sss.com/api/showme?name=${abc}
rest headers name =  custom-header; value =  ${abc}
rest body =  {name: "${abc}"}

以上是关于使用Post方法为Rest Web服务设置setRequestBody的主要内容,如果未能解决你的问题,请参考以下文章

REST服务上的错误请求错误使用POST(json数据)调用方法?

三种主流的Web服务实现

使用 API POST 到 REST Web 服务:正文序列化

使用 alamofire 将带有 JSON 对象和查询参数的 POST 请求发送到 REST Web 服务

RESTful Web 服务四种操作POST/DELETE/PUT/GET

在自托管 Web 服务器环境中通过 POST REST API 上传 C# 文件