我从如下所示的API响应中获得价值
{
"first_name" = "active EQ 'true'";
}
我需要将该值作为另一个API中的查询参数发送。但是在发送请求时,我看到URL尚未被编码。我期望active EQ 'true'
的URL编码为active%20EQ%20%27true%27
。
我尝试了addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) and it resulted in "active%20EQ%20\'true\'
。我看到反斜杠被添加为转义符,但未按预期进行编码。
let k = "active EQ 'true'"
let j = k.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
print("j is \(j)")
我希望输出为active%20EQ%20%27true%27
,但实际输出为active%20EQ%20\'true\'
答案 0 :(得分:0)
首先从您的字符串中省略“”。检查以下代码:
def iterate(collection_name, batch_size=5000, cursor=None):
query = client.collection(collection_name).limit(batch_size).order_by('__name__')
if cursor:
query = query.start_after(cursor)
for doc in query.stream():
yield doc
else:
if 'doc' in locals():
yield from iterate(collection_name, batch_size, doc)
答案 1 :(得分:0)
尝试一下,我检查了它,并且工作正常。
let k = "active EQ 'true'"
let j = escape(k)
print("j is \(j)")
func escape(_ string: String) -> String {
let generalDelimitersToEncode = ":#[]@" // does not include "?" or "/" due to RFC 3986 - Section 3.4
let subDelimitersToEncode = "!$&'()*+,;="
var allowedCharacterSet = CharacterSet.urlQueryAllowed
allowedCharacterSet.remove(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)")
return string.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet) ?? string
}
结果:j处于活动状态%20EQ%20%27true%27
答案 2 :(得分:0)
感谢所有建议和EOD,我不希望对单引号进行编码,因为API可以正常工作,并且API的值应仅使用单引号发送。我只是想知道编码这种类型的字符串的可能性。