2021年4月4日星期日

How to pass a json object as a query in a get request

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())

https://stackoverflow.com/questions/66947699/how-to-pass-a-json-object-as-a-query-in-a-get-request April 05, 2021 at 10:45AM

没有评论:

发表评论