如何使用Alamofire运行结构化查询?
如下图所示,它在Postman中效果很好。
但是,我的代码不起作用...
func getMy() {
let projectId = "lolcourt-554c8"
let url = "https://firestore.googleapis.com/v1/projects/\(projectId)/databases/(default)/documents:runQuery"
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
do {
let post = MyPost(userId: "Mr.Kim")
request.httpBody = try JSONEncoder().encode(post)
} catch {
print("http Body Error")
}
AF.request(request).responseString { response in
print(response)
}
输出:
success("[{\n \"error\": {\n \"code\": 400,\n \"message\": \"Invalid JSON payload received. Unknown name \\\"{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr.Kim\\\"}}},\\\"orderBy\\\":[{\\\"field\\\":{\\\"fieldPath\\\":\\\"date\\\"},\\\"direction\\\":\\\"DESCENDING\\\"}],\\\"from\\\":[{\\\"collectionId\\\":\\\"lolCourt\\\"}]}}\\\": Cannot bind query parameter. Field \'{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr\' could not be found in request message.\",\n \"status\": \"INVALID_ARGUMENT\",\n \"details\": [\n {\n \"@type\": \"type.googleapis.com/google.rpc.BadRequest\",\n \"fieldViolations\": [\n {\n \"description\": \"Invalid JSON payload received. Unknown name \\\"{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr.Kim\\\"}}},\\\"orderBy\\\":[{\\\"field\\\":{\\\"fieldPath\\\":\\\"date\\\"},\\\"direction\\\":\\\"DESCENDING\\\"}],\\\"from\\\":[{\\\"collectionId\\\":\\\"lolCourt\\\"}]}}\\\": Cannot bind query parameter. Field \'{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr\' could not be found in request message.\"\n }\n ]\n }\n ]\n }\n}\n]")
我认为编码完成了没有任何错误。
if let encoded = try? JSONEncoder().encode(MyPost(userId: "Mr.Kim")) {
print(String(data: encoded, encoding: .utf8)!)
}
输出:
{"structuredQuery":{"where":{"fieldFilter":{"field":{"fieldPath":"userID"},"op":"EQUAL","value":{"stringValue":"Mr.Kim"}}},"orderBy":[{"field":{"fieldPath":"date"},"direction":"DESCENDING"}],"from":[{"collectionId":"lolCourt"}]}}
As you can see in the image below, It works well in PostMan.
But, my code didn't work...
func getMy() {
let projectId = "lolcourt-554c8"
let url = "https://firestore.googleapis.com/v1/projects/\(projectId)/databases/(default)/documents:runQuery"
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
do {
let post = MyPost(userId: "Mr.Kim")
request.httpBody = try JSONEncoder().encode(post)
} catch {
print("http Body Error")
}
AF.request(request).responseString { response in
print(response)
}
Output:
success("[{\n \"error\": {\n \"code\": 400,\n \"message\": \"Invalid JSON payload received. Unknown name \\\"{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr.Kim\\\"}}},\\\"orderBy\\\":[{\\\"field\\\":{\\\"fieldPath\\\":\\\"date\\\"},\\\"direction\\\":\\\"DESCENDING\\\"}],\\\"from\\\":[{\\\"collectionId\\\":\\\"lolCourt\\\"}]}}\\\": Cannot bind query parameter. Field \'{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr\' could not be found in request message.\",\n \"status\": \"INVALID_ARGUMENT\",\n \"details\": [\n {\n \"@type\": \"type.googleapis.com/google.rpc.BadRequest\",\n \"fieldViolations\": [\n {\n \"description\": \"Invalid JSON payload received. Unknown name \\\"{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr.Kim\\\"}}},\\\"orderBy\\\":[{\\\"field\\\":{\\\"fieldPath\\\":\\\"date\\\"},\\\"direction\\\":\\\"DESCENDING\\\"}],\\\"from\\\":[{\\\"collectionId\\\":\\\"lolCourt\\\"}]}}\\\": Cannot bind query parameter. Field \'{\\\"structuredQuery\\\":{\\\"where\\\":{\\\"fieldFilter\\\":{\\\"field\\\":{\\\"fieldPath\\\":\\\"userID\\\"},\\\"op\\\":\\\"EQUAL\\\",\\\"value\\\":{\\\"stringValue\\\":\\\"Mr\' could not be found in request message.\"\n }\n ]\n }\n ]\n }\n}\n]")
I think the encoding was completed without any errors.
if let encoded = try? JSONEncoder().encode(MyPost(userId: "Mr.Kim")) {
print(String(data: encoded, encoding: .utf8)!)
}
Output:
{"structuredQuery":{"where":{"fieldFilter":{"field":{"fieldPath":"userID"},"op":"EQUAL","value":{"stringValue":"Mr.Kim"}}},"orderBy":[{"field":{"fieldPath":"date"},"direction":"DESCENDING"}],"from":[{"collectionId":"lolCourt"}]}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与参数编码有关,只需添加此标头
This related to parameter encoding , simply add this header
无需您自己的参数编码,Alamofire具有内置。
这将正确编码任何
编码
键入JSON。PS。要打印响应json而不逃脱,我建议
print(字符串(Decoding:data,as:utf8.self)为NSString)
There's no need to do you own parameter encoding, Alamofire has that built in.
This will properly encode any
Encodable
type as JSON.PS. To print response JSON without escaping I recommend
print(String(decoding: data, as: UTF8.self) as NSString)