我正在尝试使用AFNetworking向Google Cloud Vision lib发出请求,但我收到400错误。我不确定问题出在哪里。我按照文档中的建议提出请求。
https://cloud.google.com/vision/docs/request#json_request_format
NSString *base64 = [self encodeToBase64String:[UIImage imageNamed:@"IMG_5081.JPG"]];
NSDictionary *d3 = @{@"requests":
@[
@{@"image":@{@"content":base64}},
@{@"features":@[@{@"type":@"LABEL_DETECTION",@"maxResults":@(1)}]}
]
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *functionURL = @"https://vision.googleapis.com/v1/images:annotate?key=AIzaSyBO3NtyPlLnagHEauMGpBHC603RnDS6BFM";
AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer];
AFHTTPResponseSerializer * responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer = responseSerializer;
manager.requestSerializer = requestSerializer;
[manager.requestSerializer setTimeoutInterval:50.0];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager POST:functionURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)task.response;
if (httpResponse.statusCode == 200 || httpResponse.statusCode == 201) {
NSData * data = (NSData *)responseObject;
success(YES,data,httpResponse);
}else{
failure(nil, httpResponse.statusCode);//success(NO,responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(error, error.code);
}];
我得到的错误是:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={NSLocalizedDescription=Request failed: bad request (400), NSErrorFailingURLKey=https://vision.googleapis.com/v1/images:annotate?key=AIzaSyBO3NtyPlLnagHEauMGpBHC603RnDS6BFM, com.alamofire.serialization.response.error.data=<7b0a2020 22657272 6f72223a 207b0a20 20202022 636f6465 223a2034 30302c0a 20202020 226d6573 73616765 223a2022 496e7661 6c696420 4a534f4e 20706179 6c6f6164 20726563 65697665 642e2055 6e6b6e6f 776e206e 616d6520 5c227265 71756573 74735b5d 5b666561 74757265 735d5b5d 5b6d6178 52657375 6c74735d 5c223a20 43616e6e 6f742062 696e6420 71756572 79207061 72616d65 7465722e 20466965 6c642027 72657175 65737473 5b5d5b66 65617475 7265735d 5b5d5b6d 61785265 73756c74 735d2720 636f756c 64206e6f 74206265 20666f75 6e642069 6e207265 71756573 74206d65 73736167 652e5c6e 496e7661 6c696420 4a534f4e 20706179 6c6f6164 20726563 65697665 642e2055 6e6b6e6f 776e206e 616d6520 5c227265 71756573 74735b5d 5b696d61 67655d5b 636f6e74 656e745d 5c223a20 43616e6e 6f742062 696e6420 71756572 79207061 72616d65 7465722e 20466965 6c642027 72657175 65737473 5b5d5b69 6d616765 5d5b636f 6e74656e 745d2720 636f756c 64206e6f 74206265 20666f75 6e642069 6e207265 71756573 74206d65 73736167 652e5c6e 496e7661 6c696420 4a534f4e 20706179 6c6f6164 20726563 65697665 642e2055 6e6b6e6f 776e206e 616d6520 5c227265 71756573 74735b5d 5b666561 74757265 735d5b5d 5b747970 655d5c22 3a204361 6e6e6f74 2062696e 64207175 65727920 70617261 6d657465 722e2046 69656c64 20277265 71756573 74735b5d 5b666561 74757265 735d5b5d 5b747970 655d2720 636f756c 64206e6f 74206265 20666f75 6e642069 6e207265 71756573 74206d65 73736167 652e222c 0a202020 20227374 61747573 223a2022 494e5641 4c49445f 41524755 4d454e54 222c0a20 20202022 64657461 696c7322 3a205b0a 20202020 20207b0a 20202020 20202020 22407479 7065223a 20227479 70652e67 6f6f676c 65617069 732e636f 6d2f676f 6f676c65 2e727063 2e426164 52657175 65737422 2c0a2020 20202020 20202266 69656c64 56696f6c 6174696f 6e73223a 205b0a20 20202020 20202020 207b0a20 20202020 20202020 20202022 64657363 72697074 696f6e22 3a202249 6e76616c 6964204a 534f4e20 7061796c 6f616420 72656365 69766564 2e20556e 6b6e6f77 6e206e61 6d65205c 22726571 75657374 735b5d5b 66656174 75726573 5d5b5d5b 6d617852 6573756c 74735d5c 223a2043 616e6e6f 74206269 6e642071 75657279 20706172 616d6574 65722e20 4669656c 64202772 65717565 7374735b 5d5b6665 61747572 65735d5b 5d5b6d61 78526573 756c7473 5d272063 6f756c64 206e6f74 20626520 666f756e 6420696e 20726571 75657374 206d6573 73616765 2e220a20 20202020 20202020 207d2c0a 20202020 20202020 20207b0a 20202020 20202020 20202020 22646573 63726970 74696f6e 223a2022 496e7661 6c696420 4a534f4e 20706179 6c6f6164 20726563 65697665 642e2055 6e6b6e6f 776e206e 616d6520 5c227265 71756573 74735b5d 5b696d61 67655d5b 636f6e74 656e745d 5c223a20 43616e6e 6f742062 696e6420 71756572 79207061 72616d65 7465722e 20466965 6c642027 72657175 65737473 5b5d5b69 6d616765 5d5b636f 6e74656e 745d2720 636f756c 64206e6f 74206265 20666f75 6e642069 6e207265 71756573 74206d65 73736167 652e220a 20202020 20202020 20207d2c 0a202020 20202020 2020207b 0a202020 20202020 20202020 20226465 73637269 7074696f 6e223a20 22496e76 616c6964 204a534f 4e207061 796c6f61 64207265 63656976 65642e20 556e6b6e 6f776e20 6e616d65 205c2272 65717565 7374735b 5d5b6665 61747572 65735d5b 5d5b7479 70655d5c 223a2043 616e6e6f 74206269 6e642071 75657279 20706172 616d6574 65722e20 4669656c 64202772 65717565 7374735b 5d5b6665 61747572 65735d5b 5d5b7479 70655d27 20636f75 6c64206e 6f742062 6520666f 756e6420 696e2072 65717565 7374206d 65737361 67652e22 0a202020 20202020 2020207d 0a202020 20202020 205d0a20 20202020 207d0a20 2020205d 0a20207d 0a7d0a>, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x60000003c220> { URL: https://vision.googleapis.com/v1/images:annotate?key=AIzaSyBO3NtyPlLnagHEauMGpBHC603RnDS6BFM } { Status Code: 400, Headers {
"Cache-Control" = (
private
);
"Content-Encoding" = (
gzip
);
"Content-Length" = (
350
);
"Content-Type" = (
"application/json; charset=UTF-8"
);
Date = (
"Fri, 20 Apr 2018 15:03:07 GMT"
);
Server = (
ESF
);
Vary = (
Origin,
"X-Origin",
Referer
);
"alt-svc" = (
"hq=\":443\"; ma=2592000; quic=51303433; quic=51303432; quic=51303431; quic=51303339; quic=51303335,quic=\":443\"; ma=2592000; v=\"43,42,41,39,35\""
);
"x-content-type-options" = (
nosniff
);
"x-frame-options" = (
SAMEORIGIN
);
"x-xss-protection" = (
"1; mode=block"
);
} }}
答案 0 :(得分:0)
错误看起来像JSON格式有错误。
我想这可能会有所帮助。 Response 400 from Google Vision API OCR with a base64 string of specified image