如何在 IDE 的查询负载中转义 & 号
how to escape ampersand in query payload in IDE
我必须将以下字符串添加到 restdb 查询中:
&sort=_changed%26dir=-1
所以它看起来像这样(在 Postman 中有效):
{"identifier": "impeachmentsage"}&sort=_changed&dir=-1
我尝试了几种转义 & 的方法,包括 %26 和 & 字符后跟 amp; ,但无论我收到一条错误消息,抱怨 & 的存在。 https://bixbydevelopers.com/dev/docs/reference/JavaScriptAPI/http#http-options
function getNewsByName(altBrainsNames, $vivContext) {
console.log('viv', $vivContext, altBrainsNames)
dashbot.logIncoming("Getting news by name", "GetNewsByName", $vivContext, altBrainsNames);
const url = properties.get("config", "baseUrl") + "content"
console.log("i got to restdb.js and the url is ", url);
console.log("AltBrainsNames is", altBrainsNames)
ampersand = "&"
const query = {
apikey: properties.get("secret", "apiKey"),
q: "{\"" + "identifier" + "\":\"" + altBrainsNames + "\"}" + ampersand+ "sort=_changed%26dir=-1"
}
console.log("query", query)
const options = {
format: "json",
query: query,
cacheTime: 0
}
尝试了一些额外的东西。
var s = "sort=_changed%26dir=-1"
const query = {
apikey: properties.get("secret", "apiKey"),
q: "{\"" + "identifier" + "\":\"" + altBrainsNames + "\"}",
s: "sort=_changed%26dir=-1"
}
它靠得更近并发出 & 符号,但它后面的 s= 是无关紧要的。
更新:
所以我也尝试构建完整的URL并通过getURL中的url参数提交,绕过查询选项
const url = properties.get("config", "baseUrl") + "content";
const q = "q={\"" + "identifier" + "\":\"" + altBrainsNames + "\"}";
const ampersand = encodeURIComponent("&");
submiturl = url + "\?" + q + ampersand + "sort=_changed" + ampersand + "dir=-1"
console.log('submit url is', submiturl)
const options = {
format: "json",
headers: {
'apikey': properties.get("secret", "apiKey"),
},
cacheTime: 0
}
console.log(options)
const response = http.getUrl(submiturl, options)
这会产生 400 错误 "unexpected & in JSON at column 32"。
我现在怀疑根本问题是getURL坚持查询字符串必须是匹配的key/value对,而restdb的语法提供了不平衡的blank/value对,即 &sort... 而不是 &s=sort。如果是这种情况,getURL 或 restdb 需要更改一些内容...
您的 URL 查询参数应该只是一个 JSON 对象,"query" 位于选项对象中 - Bixby 将处理所有需要的转义等
例如:
let url = "https://postman-echo.com/get"
let options = {
format: 'json',
query: {
identifier: "impeachmentsage",
sorts: "_changed",
dir: -1,
blank: "",
withAmper: "hello&world"
}
};
let response = http.getUrl(url, options)
console.log ("response = " + JSON.stringify(response));
导致以下 URL 被调用
https://postman-echo.com/get?identifier=impeachmentsage&sorts=_changed&dir=-1&blank=&withAmper=hello%26world
我添加了 "blank" 作为传递空 string/null 和 "withAmper" 的示例,表明 Bixby 会为您执行任何必要的转义。
仅供参考 - 如果您需要转义 URL,Javascript encodeURI
中的构建效果很好
非常感谢@rogerkibbe!在这里发布我的最终代码,因为有一个或两个小调整以使其完全符合 restdb 的格式。
const url = properties.get("config", "baseUrl") + "content";
const q = "{\"" + "identifier" + "\":\"" + altBrainsNames + "\"}";
const options = {
format: "json",
headers: {
'apikey': properties.get("secret", "apiKey"),
},
query: {
q: q,
//identifier: "impeachmentsage",
sort: "_changed",
dir: -1,
//blank: "",
//withAmper: "hello&world"
},
cacheTime: 0
}
我必须将以下字符串添加到 restdb 查询中:
&sort=_changed%26dir=-1
所以它看起来像这样(在 Postman 中有效):
{"identifier": "impeachmentsage"}&sort=_changed&dir=-1
我尝试了几种转义 & 的方法,包括 %26 和 & 字符后跟 amp; ,但无论我收到一条错误消息,抱怨 & 的存在。 https://bixbydevelopers.com/dev/docs/reference/JavaScriptAPI/http#http-options
function getNewsByName(altBrainsNames, $vivContext) {
console.log('viv', $vivContext, altBrainsNames)
dashbot.logIncoming("Getting news by name", "GetNewsByName", $vivContext, altBrainsNames);
const url = properties.get("config", "baseUrl") + "content"
console.log("i got to restdb.js and the url is ", url);
console.log("AltBrainsNames is", altBrainsNames)
ampersand = "&"
const query = {
apikey: properties.get("secret", "apiKey"),
q: "{\"" + "identifier" + "\":\"" + altBrainsNames + "\"}" + ampersand+ "sort=_changed%26dir=-1"
}
console.log("query", query)
const options = {
format: "json",
query: query,
cacheTime: 0
}
尝试了一些额外的东西。
var s = "sort=_changed%26dir=-1"
const query = {
apikey: properties.get("secret", "apiKey"),
q: "{\"" + "identifier" + "\":\"" + altBrainsNames + "\"}",
s: "sort=_changed%26dir=-1"
}
它靠得更近并发出 & 符号,但它后面的 s= 是无关紧要的。
更新:
所以我也尝试构建完整的URL并通过getURL中的url参数提交,绕过查询选项
const url = properties.get("config", "baseUrl") + "content";
const q = "q={\"" + "identifier" + "\":\"" + altBrainsNames + "\"}";
const ampersand = encodeURIComponent("&");
submiturl = url + "\?" + q + ampersand + "sort=_changed" + ampersand + "dir=-1"
console.log('submit url is', submiturl)
const options = {
format: "json",
headers: {
'apikey': properties.get("secret", "apiKey"),
},
cacheTime: 0
}
console.log(options)
const response = http.getUrl(submiturl, options)
这会产生 400 错误 "unexpected & in JSON at column 32"。
我现在怀疑根本问题是getURL坚持查询字符串必须是匹配的key/value对,而restdb的语法提供了不平衡的blank/value对,即 &sort... 而不是 &s=sort。如果是这种情况,getURL 或 restdb 需要更改一些内容...
您的 URL 查询参数应该只是一个 JSON 对象,"query" 位于选项对象中 - Bixby 将处理所有需要的转义等
例如:
let url = "https://postman-echo.com/get"
let options = {
format: 'json',
query: {
identifier: "impeachmentsage",
sorts: "_changed",
dir: -1,
blank: "",
withAmper: "hello&world"
}
};
let response = http.getUrl(url, options)
console.log ("response = " + JSON.stringify(response));
导致以下 URL 被调用
https://postman-echo.com/get?identifier=impeachmentsage&sorts=_changed&dir=-1&blank=&withAmper=hello%26world
我添加了 "blank" 作为传递空 string/null 和 "withAmper" 的示例,表明 Bixby 会为您执行任何必要的转义。
仅供参考 - 如果您需要转义 URL,Javascript encodeURI
中的构建效果很好
非常感谢@rogerkibbe!在这里发布我的最终代码,因为有一个或两个小调整以使其完全符合 restdb 的格式。
const url = properties.get("config", "baseUrl") + "content";
const q = "{\"" + "identifier" + "\":\"" + altBrainsNames + "\"}";
const options = {
format: "json",
headers: {
'apikey': properties.get("secret", "apiKey"),
},
query: {
q: q,
//identifier: "impeachmentsage",
sort: "_changed",
dir: -1,
//blank: "",
//withAmper: "hello&world"
},
cacheTime: 0
}