我正在尝试将参数传递给需要参数之一是 json 对象的 url get 请求。我正在使用 Google 应用程序脚本。我收到无效参数异常。searchConditions 参数需要 json。我还需要在查询之后传递一些额外的参数来指定要返回的字段。
文档中的示例如下所示:
https://theurl/search?searchConditions=[{ "Param1": "value", "Param2": "value"}]&fields=[ "Field1", "Field2", "etc"]
我的代码如下:
var url = "https:/theurl/search?searchConditions=";
var conditions = {
"Param1":"value",
"Param2":"value"
};
var fields = "&fields=[\'Field1\',\'Field2\',\'etc\']";
var options = {
"method":"Get",
"Accept": "application/json",
"Content-Type": "application/json",
"muteHttpExceptions" : true
};
options.headers = {"Authorization": "Basic xxxxxxxxxxxxx="};
var jsondata = UrlFetchApp.fetch(url + conditions + fields, options);
var data = JSON.parse(jsondata.getContentText())
更新:来自文档:
搜索事件
URI https://trackerbeyond.phaseware.com:443/pro9/api/incident/search
HTTP 动词 GET
请求参数 有两个请求参数作为 URI 的一部分传入;搜索条件和字段。
searchConditions 参数 这是一个搜索条件数组。请注意,当定义多个条件时,必须满足所有条件才能返回事件。
此表显示要在每个搜索条件中设置的字段。字段名称 说明 ConditionType 可能的值:Exact、BeginsWith、Contains、DateRange、IsBlank、IsNotBlank、ArchiveStatus、OpenCloseStatus。FieldName 要搜索的字段的名称。要获取完整的字段列表,请使用事件架构 API 调用。TableName 字段所在的表的名称。可能的值为 z_Incident、UserIncident、z_FullTextMain、Customer、UserCustomer、Contacts、UserContacts、CustomerContacts、UserCustomerContacts、CustomerProducts、CustomerSupportPackages、IncidentParts、IncidentParties、DepartmentGroupingMembers(用于在 DepartmentGrouping 上进行搜索)。如果为空,则将使用 z_Incident。注意:对于 CreateDate 和 Description 字段,使用 z_FullTextMain 作为 TableName。Lookup 一个包含两个字段的对象;LookupTableName 和 LookupFieldName。SearchValue 要搜索的值。EndingSearchValue 在指定 DateRange 的 ConditionType 以提供结束日期时使用。OpenActive 可能的值:All、OpenOrActive、ClosedOrArchived。请注意,此参数仅适用于添加条件类型为 OpenCloseStatus 的搜索条件。
示例 在此示例中,第一个搜索条件是搜索 DepartmentID 等于 1。第二个条件是搜索“Priority 3”的 Priority 描述。这是使用 Lookup 字段搜索描述而不是 id 的示例。第三个条件是包括开放事件和封闭事件。默认情况下,仅返回打开的事件。[ { "ConditionType": "Exact", "FieldName": "DepartmentID", "TableName": "", "Lookup": "", "SearchValue": "1", "EndingSearchValue": "", "OpenActive" : "OpenOrActive" }, { "ConditionType": "Exact", "FieldName": "PriorityID", "TableName": "", "
fields 参数 这是要在结果中返回的字段数组。如果此参数留空,则将返回一组默认字段。
从测试这个 API 接口成功调用:https://trackerbeyond.phaseware.com:443/pro9/api/incident/search?searchConditions=[ { "ConditionType": "Exact", "FieldName": "StatusID", "SearchValue": "12", "OpenActive": "OpenOrActive" } ]&fields=[ "CustomerName", "CreateDate", "AgentFullName", "ClosedDateTime", "StatusID", "IncidentID", "Description" ]
contentType
不需要使用。Accept
,请将其包含在请求标头中。The example in the documentation looks like this: https://theurl/search?searchConditions=[{ "Param1": "value", "Param2": "value"}]&fields=[ "Field1", "Field2", "etc"]
您的问题来看,我认为在这种情况下,如何对查询参数的值进行 URL 编码?当以上几点反映到你的脚本中时,它变成如下。
var url = "https:/theurl/search";
var conditions = [{ "Param1": "value", "Param2": "value" }];
var fields = ["Field1", "Field2", "etc"];
var options = {
"method": "get",
"headers": {
"Authorization": "Basic xxxxxxxxxxxxx=",
"Accept": "application/json",
},
"muteHttpExceptions": true
};
var jsondata = UrlFetchApp.fetch(`${url}?searchConditions=${encodeURIComponent(JSON.stringify(conditions))}&fields=${encodeURIComponent(JSON.stringify(fields))}`, options);
var data = JSON.parse(jsondata.getContentText())
https:/theurl/search
和令牌"Basic xxxxxxxxxxxxx="
可用于您要使用的 API。请注意这一点。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句