I am trying to pass parameters to a url get request that requires one of the parameters be a json object. I am using Google apps script. I get an invalid argument exception. The searchConditions parameter requires the json. I also need to pass some additional parameters after the query to specify the fields to return.
The example in the documentation looks like this:
https://theurl/search?searchConditions=[{ "Param1": "value", "Param2": "value"}]&fields=[ "Field1", "Field2", "etc"]
My code is as follows:
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())
Update: From Documentation:
Search Incidents
URI https://trackerbeyond.phaseware.com:443/pro9/api/incident/search
HTTP Verb GET
Request Parameters There are two request parameters to pass in as part of the URI; searchConditions and fields.
searchConditions Parameter This is an array of search conditions. Note that when multiple conditions are defined, all conditions must be met for the Incident to be returned.
This table shows the fields to set within each search condition. Fieldname Description ConditionType Possible values: Exact, BeginsWith, Contains, DateRange, IsBlank, IsNotBlank, ArchiveStatus, OpenCloseStatus. FieldName The name of the field to search against. To get the full list of fields use the Incident Schema API call. TableName The name of the table that the field exists on. Possible values are z_Incident, UserIncident, z_FullTextMain, Customer, UserCustomer, Contacts ,UserContacts ,CustomerContacts ,UserCustomerContacts ,CustomerProducts, CustomerSupportPackages ,IncidentParts ,IncidentParties ,DepartmentGroupingMembers (for searching on a DepartmentGrouping). If blank then z_Incident will be used. Note: For the CreateDate and Description fields use z_FullTextMain for the TableName. Lookup An object that contains two fields; LookupTableName and LookupFieldName. SearchValue The value to search on. EndingSearchValue Used when specifying ConditionType of DateRange to supply the ending date. OpenActive Possible values: All, OpenOrActive, ClosedOrArchived. Please note that this parameter only applies when adding a search condition with ConditionType of OpenCloseStatus.
Example In this example, the first search condition is for searching on DepartmentID equal to 1. The second condition is for searching on Priority description of "Priority 3". This is an example of using the Lookup field to search on a description for a lookup instead of the id. The third condition is for including both open and closed incidents. By default only open incidents are returned. [ { "ConditionType": "Exact", "FieldName": "DepartmentID", "TableName": "", "Lookup": "", "SearchValue": "1", "EndingSearchValue": "", "OpenActive": "OpenOrActive" }, { "ConditionType": "Exact", "FieldName": "PriorityID", "TableName": "", "Lookup": { "LookupTableName": "LU_Priority", "LookupFieldName": "Description" }, "SearchValue": "Priority 3", "EndingSearchValue": "", "OpenActive": "OpenOrActive" }, { "ConditionType": "OpenCloseStatus", "FieldName": "", "TableName": "", "Lookup": "", "SearchValue": "1", "EndingSearchValue": "", "OpenActive": "All" } ]
fields Parameter This is an array of fields to return in the result. If this parameter is left blank then a default set of fields will be returned.
Successful call made from the Test this API interface: 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
is not required to be used.Accept
, please include it in the request header.The example in the documentation looks like this: https://theurl/search?searchConditions=[{ "Param1": "value", "Param2": "value"}]&fields=[ "Field1", "Field2", "etc"]
in your question, I think that in this case, how about URL encoding the values of query parameter?When above points are reflected to your script, it becomes as follows.
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
and the token of "Basic xxxxxxxxxxxxx="
can be used for the API you want to use. Please be careful this.