【发布时间】:2018-04-04 11:17:28
【问题描述】:
尝试按照link 中的说明从我的 iOS 应用创建请求,但收到 Invalid JSON payload received 和 INVALID_ARGUMENT 错误消息。有什么建议吗?
这是我尝试使用的示例。
{
"requests":[
{
"image":{
"source":{
"imageUri":
"https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
}
},
"features":[
{
"type":"LOGO_DETECTION",
"maxResults":1
}
]
}
]
}
在我的 iOS 应用中,我创建了 para
let feature1 = ["type": "LOGO_DETECTION", "maxResults": 1] as [String : Any]
let features = [feature1]
let source = ["imageUri": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"]
let image = ["source": source]
let request = ["image": image, "features": features] as [String : Any]
let requests = ["requests": [request]]
if let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print(str)
}
let privateKey = "my............key"
let requestString = "https://vision.googleapis.com/v1/images:annotate?key=\(privateKey)"
Alamofire.request(requestString, method: .post, parameters: requests).responseString { (response) in
switch response.result {
case .success(let data):
print("data", data)
print(response.result)
case .failure(let error):
print(error)
}
}
我可以看到我的打印日志中有相同的内容。
{
"requests" : [
{
"features" : [
{
"type" : "LOGO_DETECTION",
"maxResults" : 1
}
],
"image" : {
"source" : {
"imageUri" : "https:\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_272x92dp.png"
}
}
}
]
}
但我收到以下错误消息
[BoringSSL] Function boringssl_context_get_peer_sct_list: line 1754 received sct extension length is less than sct data length
data {
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"requests[][features][][maxResults]\": Cannot bind query parameter. Field 'requests[][features][][maxResults]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[][image][source][imageUri]\": Cannot bind query parameter. Field 'requests[][image][source][imageUri]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[][features][][type]\": Cannot bind query parameter. Field 'requests[][features][][type]' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"requests[][features][][maxResults]\": Cannot bind query parameter. Field 'requests[][features][][maxResults]' could not be found in request message."
},
{
"description": "Invalid JSON payload received. Unknown name \"requests[][image][source][imageUri]\": Cannot bind query parameter. Field 'requests[][image][source][imageUri]' could not be found in request message."
},
{
"description": "Invalid JSON payload received. Unknown name \"requests[][features][][type]\": Cannot bind query parameter. Field 'requests[][features][][type]' could not be found in request message."
}
]
}
]
}
}
正确答案
1.删除
if let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
let str = String(data: data, encoding: .utf8) {
print(str)
}
2.改变
Alamofire.request(requestString, method: .post, parameters: requests).responseString { (response) in
到
Alamofire.request(requestString, method: .post, parameters: requests, encoding: JSONEncoding.default, headers: [:]).responseJSON { (response) in
【问题讨论】:
标签: ios json google-api http-post google-vision